我正在使用Jetpack Compose创建一个简单的闪存卡.

Unfortunately... I could not even find the official documentation, so my learning style has been trusting the autocorrect system...

无论如何,我相信问题要么是Box()的问题,要么是text()的问题. 我已经为长方体的重力添加了Align.CenterEnd.然而,就盒子而言,这似乎是唯一的居中方式.另一方面,text()没有给出任何方法来实现这一点(它很有吸引力,但似乎没有改变任何东西)

A hand in the right direction would be amazing.

顺便说一句,我知道这将是免费的回答.但是我该如何更改on click中$question的文本呢.正如我所认为的那样,复合物能让人耳目一新?...因此,应该在屏幕上重新生成吗?也许不是?

谢谢!

 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,
                )
            }
        }

Picture of Current Content

推荐答案

您可以在Text中申请textAlign = TextAlign.Center:

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,
        )

enter image description here

关于文本.

您可以使用以下内容:

var text by remember { mutableStateOf(("How many cars are in the garage?")) }

In your clickable item:

.clickable(onClick = { text= "After clicking"} )

in your Text:

  Text(text, 
        textAlign = TextAlign.Center,
        ...)

这只是一个简单的问题.可以使用动态 struct 来存储和更新值,而不是使用静态字符串.

Kotlin相关问答推荐

Kotlin—列出具有不同T的列表之间的操作'

Kotlin协程挂起继续线程

Groovy Gradle文件的Kotlin类似功能

无法访问类kotlin.coroutines.CoroutineContext';.判断模块类路径中是否存在丢失或冲突的依赖项

&x是T&q;和&q;(x为?T)!=空(&Q;)?

Scala与Kotlin中的迭代

Kotlin 可空泛型

为 Gradle 子项目配置 Kotlin 扩展

Kotlin 可打包类抛出 ClassNotFoundException

Picasso 回调

kotlin,如何从函数返回类类型

retrofit 响应代码 405 并带有消息method not allowed here

如何在 kotlin 的片段类中更改 ActionBar 标题?

将协同路由调用放在存储库或ViewModel中哪个更好?

Kotlin suspend fun

将多个 Kotlin 流合并到一个列表中,而无需等待第一个值

Kotlin Native如何将字节数组转换为字符串?

(kotlin的Moshi)@Json vs@field:Json

Kotlin数据类打包

Kotlin中的函数接口