Android Jetpack Compose: la familia de fuentes predeterminada no se aplica al texto()
Configuré la familia de fuentes Cursive como la predeterminada en el archivo Type.kt:
package com.example.composeproject.ui.theme
import androidx.compose.material.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
val Typography = Typography(
defaultFontFamily = FontFamily.Cursive, // the default is cursive font
body1 = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)
/* Other default text styles to override
button = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.W500,
fontSize = 14.sp
),
caption = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 12.sp
)
*/
)
Archivo Theme.kt:
package com.example.composeproject.ui.theme
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Shapes
import androidx.compose.material.darkColors
import androidx.compose.material.lightColors
import androidx.compose.runtime.Composable
private val DarkColorPalette = darkColors(
primary = Purple80,
secondary = PurpleGrey80
)
private val LightColorPalette = lightColors(
primary = Purple40,
secondary = PurpleGrey40
/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F),
*/
)
@Composable
fun ComposeProjectTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes(),
content = content
)
}
Código MainActivity.kt:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.ui.Modifier
import com.example.composeproject.ui.theme.ComposeProjectTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeProjectTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Column {
Text(
text = "Test Text"
)
Button(onClick = { /*TODO*/ }) {
Text(text = "Click Me")
}
}
//MainScreen()
}
}
}
}
}
La salida se ve así:
Como puede ver, la cursiva se aplica al Button()
texto, pero no la Text()
. ¿Alguien se enfrenta al mismo problema?
Aceptado
Inmediatamente anula body1
el estilo que utiliza with Text
, fontFamily = FontFamily.Default
de ahí el estilo predeterminado. Button
utiliza button
un estilo de texto que no se anula.
Puede eliminar body1
la anulación por completo o definirla fontFamilty
también:
defaultFontFamily = FontFamily.Cursive,
body1 = TextStyle(
fontFamily = FontFamily.Cursive,
fontWeight = FontWeight.Normal,
fontSize = 16.sp
)