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

Jetpack Compose中的左对齐按钮文本

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

    我正在使用以下代码创建 OutlinedButton 在Jetpack Compose中:

    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 = {}
            ) {
    
                Text(
                    modifier = Modifier.padding(horizontal = 0.dp, vertical = 0.dp),
                    color = Color.Black,
                    text = text,
                    textAlign = TextAlign.Left,
                    fontSize = 15.sp,
                )
            }
    

    我试着用 TextAlign.Left TextAlign.Start 。但是,它总是在中心对齐:

    enter image description here

    如何解决这个问题?

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

    在按钮内添加行

    OutlinedButton(onClick = {  }, modifier = Modifier
            .fillMaxWidth()
            .height(48.dp)) {
    
            Row(modifier = Modifier.fillMaxWidth()) {
                Text(text = "Button Text", textAlign = TextAlign.Start)
    
            }
    
    
        }
    
        2
  •  0
  •   Abhimanyu    2 年前

    使用 fillMaxWidth() 在…上 Text 对齐方式为 Left

    @Composable
    fun LeftAlignSample() {
        OutlinedButton(
            onClick = {},
        ) {
            Text(
                "Text",
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.Left,
            )
        }
    }