Pan*_*All 5 android android-layout kotlin android-jetpack-compose
我正在使用 Jetpack Compose 创建一个简单的闪存卡。这个想法是你点击闪存卡,它会给你答案。但是,我被困在一个基本问题上。
不幸的是......我什至找不到官方文档,所以我的学习风格一直相信自动更正系统......
无论如何,我相信问题出在 Box() 或 Text() 上。我为盒子的重力添加了一个 Align.CenterEnd 。但是,这似乎是框居中的唯一方法。另一方面, Text() 没有提供任何方法来这样做(它有重力,但似乎没有改变任何东西)
方向正确的手将是惊人的。
附带说明一下,我知道这将提供免费答案。但是我将如何在点击时更改 $question 的文本。正如我所认为的 Composables 刷新?...因此,应该在屏幕上重新生成?也许不吧?
谢谢!
val typography = MaterialTheme.typography
val context = ContextAmbient.current
var question = "How many Bananas should go in my Smoothie?"
Column(modifier = Modifier.padding(30.dp).then(Modifier.fillMaxWidth())
.then(Modifier.wrapContentSize(Alignment.Center))
.clickable(onClick = { Toast.makeText(context, "3 Bananas are needed!", Toast.LENGTH_LONG).show()} ) /*question = "3 Bananas required"*/
.clip(shape = RoundedCornerShape(16.dp))) {
Box(modifier = Modifier.preferredSize(350.dp)
.gravity(align = Alignment.CenterHorizontally)
.border(width = 4.dp, color = Gray, shape = RoundedCornerShape(16.dp)),
shape = RoundedCornerShape(2.dp),
backgroundColor = DarkGray,
gravity = Alignment.CenterEnd) {
Text("$question",
style = typography.h4,
)
}
}
Run Code Online (Sandbox Code Playgroud)
Pha*_*inh 64
您可以在可组合项中使用textAlign水平对齐文本和wrapContentHeight垂直对齐文本Text
示例中的文本居中(水平和垂直)Text
Text(
text = "How many cars are in the garage",
textAlign = TextAlign.Center, // make text center horizontal
modifier = Modifier
.width(150.dp)
.height(150.dp)
.background(Color.Cyan)
.wrapContentHeight() // make text center vertical
)
Run Code Online (Sandbox Code Playgroud)
示例将底部与中心水平对齐Text
Text(
text = "How many cars are in the garage",
textAlign = TextAlign.Center, // make text center horizontal
modifier = Modifier
.width(150.dp)
.height(150.dp)
.background(Color.Cyan)
.wrapContentHeight(Alignment.Bottom) // align bottom
)
Run Code Online (Sandbox Code Playgroud)
示例将底部与水平中心对齐Box
Box(modifier = Modifier
.width(150.dp)
.height(150.dp)
.background(Color.Cyan),
contentAlignment = Alignment.BottomCenter
) {
Text(
text = "How many cars are in the garage",
textAlign = TextAlign.Center
)
}
Run Code Online (Sandbox Code Playgroud)
Gab*_*tti 18
您可以textAlign = TextAlign.Center在Text:
Column(modifier = Modifier
.padding(30.dp)
.fillMaxWidth()
.wrapContentSize(Alignment.Center)
.clickable(onClick = { } ) /*question = "3 Bananas required"*/
.clip(shape = RoundedCornerShape(16.dp)),
) {
Box(modifier = Modifier
.preferredSize(350.dp)
.border(width = 4.dp, color = Gray, shape = RoundedCornerShape(16.dp)),
alignment = Alignment.Center) {
Text("Question 1 : How many cars are in the garage?",
Modifier.padding(16.dp),
textAlign = TextAlign.Center,
style = typography.h4,
)
Run Code Online (Sandbox Code Playgroud)
关于正文。
您可以使用以下内容:
var text by remember { mutableStateOf(("How many cars are in the garage?")) }
Run Code Online (Sandbox Code Playgroud)
在您的可点击项目中:
.clickable(onClick = { text= "After clicking"} )
Run Code Online (Sandbox Code Playgroud)
在您的文本中:
Text(text,
textAlign = TextAlign.Center,
...)
Run Code Online (Sandbox Code Playgroud)
这只是一个简单的。您可以使用动态结构来存储和更新值,而不是静态字符串。
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.fillMaxHeight()
){
Text(
text = "Text",
textAlign = TextAlign.Center
)
}
Run Code Online (Sandbox Code Playgroud)