代码之家  ›  专栏  ›  技术社区  ›  nikhilbalwani

Jetpack Compose中同一行按钮内的文本同时左对齐和右对齐

  •  0
  • nikhilbalwani  · 技术社区  · 2 年前

    我正在尝试在一个按钮中添加两个文本部分。第一个文本向左对齐,而第二个文本应该向右对齐。以下是我尝试过的:

    OutlinedButton(
                colors = ButtonDefaults.outlinedButtonColors(Color.Transparent),
                border = BorderStroke(0.dp, Color.Transparent),
                modifier = Modifier.fillMaxWidth()
                                   .background(
                    color = Color.Transparent,
            shape = RectangleShape, // Oval shape
    
            //border = androidx.compose.ui.drawBorder(2.dp, Color.Red) // Border color and width
            ),
                onClick = {}
            ) {
                Row(modifier = Modifier.fillMaxWidth()) {
                    Text(
                        modifier = Modifier.padding(horizontal = 0.dp, vertical = 0.dp),
                        color = Color.Black,
                        text = text,
                        textAlign = TextAlign.Start,
                        fontSize = 15.sp,
                    )
                    Text(
                        modifier = Modifier.padding(horizontal = 0.dp, vertical = 0.dp),
                        color = Color.Black,
                        text = ">",
                        textAlign = TextAlign.End,
                        fontSize = 15.sp,
                    )
                }
            }
    

    然而,这并没有给我带来预期的行为。它将两个文本向左对齐:

    enter image description here

    如何解决这个问题?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Sachin    2 年前

    添加水平排列间距,即可

     OutlinedButton(
            onClick = { }, modifier = Modifier
                .fillMaxWidth()
                .height(48.dp)
        ) {
    
            Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
                Text(text = "Button Text1", textAlign = TextAlign.Start)
                Text(text = "Button Text2", textAlign = TextAlign.End)
    
            }
    
    
        }