android compose BasicTextField内容居中
要使BasicTextField中的文本内容居中,可以使用modifier参数来应用样式。
以下是一个示例代码:
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.material.TextField
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.runtime.Composable
@Composable
fun CenteredTextField() {
var text by remember { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
textStyle = TextStyle(fontSize = 18.sp, color = Color.Black),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Text),
textAlign = TextAlign.Center
)
}
在上面的代码中,我们使用了textAlign参数将文本内容设置为居中对齐。这将使输入框中的文本内容居中显示
原文地址: https://www.cveoy.top/t/topic/ifYj 著作权归作者所有。请勿转载和采集!