openplanning

Hướng dẫn và ví dụ Android TextView

Xem thêm các chuyên mục:

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người.
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- Android TextView

Trong Android, TextView là một điều khiển giao diện người dùng (User Interface Control), được sử dụng để hiển thị một văn bản, nó hành động giống như một nhãn (Label), theo mặc định người dùng sẽ không thể hiệu chỉnh nội dung văn bản.

2- TextView Attributes

Attribute Description
android:id It is used to uniquely identify the control
android:autoLink It will automatically found and convert URL(s) and email addresses as clickable links.
android:ems It is used to make the TextView be exactly this many ems wide.
android:hint It is used to display the hint text when text is empty
android:width It makes the TextView be exactly this many pixels wide.
android:height It makes the TextView be exactly this many pixels tall.
android:text It is used to display the text.
android:textColor It is used to change the color of the text.
android:gravity It is used to specify how to align the text by the view's x and y-axis.
android:maxWidth It is used to make the TextView be at most this many pixels wide.
android:minWidth It is used to make the TextView be at least this many pixels wide.
android:textSize It is used to specify the size of the text.
android:textStyle It is used to change the style (bold, italic, bolditalic) of text.
android:textAllCaps It is used to present the text in all CAPS.
android:typeface It is used to specify the Typeface (normal, sans, serif, monospace) for the text.
android:textColorHighlight It is used to change the color of text selection highlight.
android:textColorLink It is used to change the text color of links.
android:inputType It is used to specify the type of text being placed in text fields.
android:fontFamily It is used to specify the fontFamily for the text.
android:editable If we set, it specifies that this TextView has an input method.
android:autoLink
Thuộc tính này nói với Android tự động tạo ra các liên kết (link) cho các số điện thoại, địa chỉ, địa chỉ email, địa chỉ website tìm thấy trên văn bản của TextView.
Constant Value Description
all f Match all patterns (equivalent to web|email|phone|map).
email 2 Match email addresses.
map 8 Match map addresses.
none 0 Match no patterns (default).
phone 4 Match phone numbers.
web 1 Match Web URLs

<TextView
    android:id="@+id/textView_a11"
    android:autoLink="email|phone|map|web"
    android:textColorLink="#FF0000"
    android:text="Contact: +1-1111222233, [email protected]. http://abc.com, 620 Eighth Avenue New York, NY 10018"
    ... />
Click on Phone Link.
Click on Email Link
Click on Web URL
Click on Address Link (Will open Google Map)
android:ems
Thuộc tính này được sử dụng để chỉ định một giá trị cho chiều rộng của TextView theo đơn vị tính EM. Trong đó EM là một đơn vị tính trong Typography (Hình chữ), giá trị của nó là chiều rộng của chữ cái "M".
Như vậy android:ems="5" nghĩa là sét đặt chiều rộng cho TextView bằng 5 lần chiều rộng của chữ cái "M".
android:text
Chỉ định một nội dung văn bản cho TextView.
android:textColor
Chỉ định mầu chữ cho TextView.
android:textSize
Chỉ định kích thước phông chữ cho văn bản.
android:textStyle
Chỉ định kiểu dáng cho phông chữ, thuộc tính này có 4 giá trị:
  1. android:textStyle="normal"  (Default)
  2. android:textStyle="bold"
  3. android:textStyle="italic"
  4. android:textStyle="bold|italic"
android:textAllCaps
Thuộc tính này có hai giá trị true/false. Với giá trị true văn bản của TextView sẽ biến thành chữ hoa khi hiển thị. Giá trị mặc định là false.
android:textColorHighlight
Thiết lập mầu nền cho văn bản con được lựa chọn. Thuộc tính này có ích khi bạn sử dụng EditText (Một lớp con của TextView). EditText cho phép người dùng lựa chọn và sửa đổi văn bản của nó.
android:textColorLink
Chỉ định mầu chữ cho các liên kết (link) trong văn bản. (Xem thêm về thuộc tính android:autoLink).
android:fontFamily
Chỉ định font family (Bộ phông chữ) cho văn bản của TextView.
android:editable

Xem thêm các chuyên mục: