Метод copy
Метод copy класса
Typography
сохраняет все стили,
кроме переданных аргументов.
Аргументы имеют тип
TextStyle:
например bodyLarge, titleMedium,
labelLarge.
Результат передают в
MaterialTheme.
Синтаксис
val typo = Typography().copy(bodyLarge = TextStyle(fontSize = 20.sp))
Параметры
| Параметр | Описание |
|---|---|
displayLarge |
Стиль самого крупного заголовка. Необязательный параметр. |
headlineMedium |
Стиль среднего заголовка экрана. Необязательный параметр. |
bodyLarge |
Стиль основного текста. Необязательный параметр. |
labelLarge |
Стиль подписи на кнопках. Необязательный параметр. |
Пример
Давайте увеличим bodyLarge в теме:
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.
Пример
Давайте сделаем headlineMedium жирнее:
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.text.font.FontWeight
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
val typo = MaterialTheme.typography.copy(
headlineMedium = MaterialTheme.typography.headlineMedium.copy(
fontWeight = FontWeight.Bold,
),
)
MaterialTheme(typography = typo) {
Text(
text = "12345",
style = MaterialTheme.typography.headlineMedium,
)
}
}
}
}
}
На экране заголовок "12345" станет жирным headlineMedium.
Смотрите также
-
класс
Typography,
у которого вызывают copy -
метод
copy,
которым правят один TextStyle внутри Typography -
функция
Text,
которой передают style из темы -
функция
MaterialTheme,
которой передают typography после copy