【问题标题】:Jetpack Compose layout challengeJetpack Compose 布局挑战
【发布时间】:2021-08-02 19:34:58
【问题描述】:

我正在尝试在 compose 中布局以下内容,但我似乎无法让对齐/安排正常工作。我尝试从 Row() 开始,也尝试从 Column 开始,但结果相同 - 未对齐。

所以我有以下属性:

 1. constantA = "774"
 2. operator = "+"
 3. constantB = "62"
 4. line = "-------"
 5. answer = "826"
 6. icon = "checkmark"

对齐方式需要如下:

     774
    + 52
   -----
     826 ✓ 

任何帮助或指点将不胜感激!

【问题讨论】:

    标签: android-layout android-jetpack-compose


    【解决方案1】:

    如果您想使用常规图标:

    @Preview
    @Composable
    fun OperationPreview() {
        val constantA = "774"
        val operator = "+"
        val constantB = "62"
        val line = "-------"
        val answer = "826"
    
        Row(verticalAlignment = Alignment.Bottom) {
            Column(horizontalAlignment = Alignment.End) {
                Text(text = constantA)
                Text(text = "$operator $constantB")
                Text(text = line)
                Row() {
                    Text(text = answer)
                }
            }
            Icon(
                modifier = Modifier
                    .size(10.dp)
                    .padding(bottom = 5.dp),
                imageVector = Icons.Default.Check,
                contentDescription = null
            )
        }
    
    }
    

    【讨论】:

      【解决方案2】:
      @Composable
      fun TextAlignLayout() {
          val constantA = "774" + "    "
          val operator = "+" + " "
          val constantB = "62" + "    "
          val line = "-------" + "    "
          val answer = "826"
          val icon = "✓"
      
          Column(
              modifier = Modifier
                  .padding(24.dp)
                  .fillMaxSize(),
              horizontalAlignment = Alignment.End
          ) {
              Text(constantA)
              Text("$operator$constantB")
              Text(line)
              Text("$answer $icon")
          }
      }
      

      【讨论】:

      • 编辑了第二行的输出。
      • 非常好,感谢您的修复。我忘记了 fillMaxSize ?,这导致了对齐问题
      • 将项目与大量空格对齐不是一个好主意,C. Hellmann 的答案要好得多
      • 是的,确实是更好的解决方案!
      猜你喜欢
      • 2019-05-31
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 2022-11-06
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2021-06-03
      相关资源
      最近更新 更多