如何更改 Jetpack Compose BasicTextField 文本颜色?
How to change Jectpack Compose BasicTextField's text color?
我正在尝试使用 BasicTextField
创建搜索栏。
默认文本颜色为黑色,我找不到任何更改文本颜色的选项。
我还需要更改文本大小、字体和其他属性。
@Composable
fun MyBasicTextField(){
var text by remember { mutableStateOf("Hello") }
BasicTextField(
value = text,
onValueChange = {
text = it
},
decorationBox = { ... },
modifier = Modifier
.fillMaxWidth()
.background(Color.DarkGray, CircleShape)
.padding(10.dp)
)
}
如果无法通过 BasicTextField
实现,是否有其他方法可以创建类似的视图?
我试过 TextField
但有几个问题,例如删除标签、高度、背景...
您需要 textStyle
参数。如果您更喜欢使用默认文本样式,请使用 LocalTextStyle
:
BasicTextField(
// ...
textStyle = LocalTextStyle.current.copy(color = Color.White)
)
或者您可以使用 material 样式之一:
BasicTextField(
// ...
textStyle = MaterialTheme.typography.body1.copy(color = Color.White)
)
除了其他答案,这对我添加 textStyle 属性:
textStyle = TextStyle(
color = Color.White,
fontFamily = FontFamily.SansSerif,
fontSize = 14.sp,
textAlign = TextAlign.Center,
)
这里是您可以设置的所有 TextStyle 属性:
https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextStyle
public constructor TextStyle(
color: Color,
fontSize: TextUnit,
fontWeight: FontWeight?,
fontStyle: FontStyle?,
fontSynthesis: FontSynthesis?,
fontFamily: FontFamily?,
fontFeatureSettings: String?,
letterSpacing: TextUnit,
baselineShift: BaselineShift?,
textGeometricTransform: TextGeometricTransform?,
localeList: LocaleList?,
background: Color,
textDecoration: TextDecoration?,
shadow: Shadow?,
textAlign: TextAlign?,
textDirection: TextDirection?,
lineHeight: TextUnit,
textIndent: TextIndent?
)
我正在尝试使用 BasicTextField
创建搜索栏。
默认文本颜色为黑色,我找不到任何更改文本颜色的选项。
我还需要更改文本大小、字体和其他属性。
@Composable
fun MyBasicTextField(){
var text by remember { mutableStateOf("Hello") }
BasicTextField(
value = text,
onValueChange = {
text = it
},
decorationBox = { ... },
modifier = Modifier
.fillMaxWidth()
.background(Color.DarkGray, CircleShape)
.padding(10.dp)
)
}
如果无法通过 BasicTextField
实现,是否有其他方法可以创建类似的视图?
我试过 TextField
但有几个问题,例如删除标签、高度、背景...
您需要 textStyle
参数。如果您更喜欢使用默认文本样式,请使用 LocalTextStyle
:
BasicTextField(
// ...
textStyle = LocalTextStyle.current.copy(color = Color.White)
)
或者您可以使用 material 样式之一:
BasicTextField(
// ...
textStyle = MaterialTheme.typography.body1.copy(color = Color.White)
)
除了其他答案,这对我添加 textStyle 属性:
textStyle = TextStyle(
color = Color.White,
fontFamily = FontFamily.SansSerif,
fontSize = 14.sp,
textAlign = TextAlign.Center,
)
这里是您可以设置的所有 TextStyle 属性:
https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextStyle
public constructor TextStyle(
color: Color,
fontSize: TextUnit,
fontWeight: FontWeight?,
fontStyle: FontStyle?,
fontSynthesis: FontSynthesis?,
fontFamily: FontFamily?,
fontFeatureSettings: String?,
letterSpacing: TextUnit,
baselineShift: BaselineShift?,
textGeometricTransform: TextGeometricTransform?,
localeList: LocaleList?,
background: Color,
textDecoration: TextDecoration?,
shadow: Shadow?,
textAlign: TextAlign?,
textDirection: TextDirection?,
lineHeight: TextUnit,
textIndent: TextIndent?
)