Класс TextView
Класс TextView наследует
View
и рисует одну или несколько строк.
В XML тег TextView,
строку задают android:text,
размер - android:textSize.
Из Kotlin текст меняют методом
setText
после
findViewById.
Синтаксис
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde" />
Параметры
| Параметр | Описание |
|---|---|
android:id |
Id для поиска TextView из кода. |
android:layout_width |
Ширина текстового блока. |
android:layout_height |
Высота текстового блока. |
android:text |
Строка на экране. Можно сослаться на @string/....
|
android:textSize |
Размер шрифта, обычно в sp. |
android:textColor |
Цвет текста. Принимает значение ресурса или #RRGGBB.
|
Методы
| Метод | Описание |
|---|---|
setText |
Меняет отображаемую строку из кода. |
Пример
Давайте выведем две подписи в столбец:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde"
android:textSize="20sp" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12345" />
</LinearLayout>
На экране сверху будет крупнее "abcde",
ниже - "12345".