Класс Typography
Класс Typography задаёт набор
TextStyle
для Material3.
Частые поля: displayLarge и headlineMedium
для крупных заголовков,
bodyLarge и bodyMedium
для основного текста,
labelLarge для подписей на кнопках.
Отдельный урок на каждый стиль не делают.
Стили читают через MaterialTheme.typography
или передают в
MaterialTheme.
Метод
copy
меняет отдельные поля.
Синтаксис
MaterialTheme.typography.bodyLarge
Методы
| Метод | Описание |
|---|---|
copy |
Возвращает Typography с подменой выбранных стилей. |
Пример
Давайте выведем заголовок и body разными стилями:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Column {
Text(
text = "abcde",
style = MaterialTheme.typography.headlineMedium,
)
Text(
text = "12345",
style = MaterialTheme.typography.bodyLarge,
)
}
}
}
}
}
На экране "abcde" будет крупнее,
"12345" - обычным bodyLarge из Typography.
Пример
Давайте увеличим bodyLarge через copy:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.ui.unit.sp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
val typo = MaterialTheme.typography.copy(
bodyLarge = MaterialTheme.typography.bodyLarge.copy(
fontSize = 22.sp,
),
)
MaterialTheme(typography = typo) {
Text(
text = "abcde",
style = MaterialTheme.typography.bodyLarge,
)
}
}
}
}
}
На экране текст "abcde" станет крупнее
за счёт изменённого bodyLarge.
Смотрите также
-
метод
copy,
который меняет отдельные TextStyle в наборе -
функция
Text,
которой передают style из Typography -
класс
TextStyle,
тип каждого поля Typography -
функция
MaterialTheme,
которая раздаёт typography по дереву