简体   繁体   English

在 Jetpack Compose 中对齐调整大小的 TextField

[英]Aligning a resized TextField in Jetpack Compose

I am having problem aligning a resized TextField in Jetpack Compose for Desktop.我在Jetpack Compose for Desktop 中对齐调整大小的TextField时遇到问题。 When I resize the width of the TextField , the TextField automatically adjust itself to center position on screen.当我调整TextField的宽度时, TextField会自动调整到 position 在屏幕上的中心。 I have tried using Modify.Align and it did not work.我曾尝试使用Modify.Align ,但它不起作用。

Can someone help?有人可以帮忙吗? Here is my code这是我的代码

@Composable
fun addSales(sales: Sales,
             actionChangeSales: (sales: Sales) -> Unit,
             actionQuantityInc: (() -> Unit),
             actionItemNameInc: (() -> Unit)){
    Column{
        TextField(
            value = sales.itemName,
            textStyle = TextStyle(color = Color.DarkGray),
            onValueChange = {
                actionChangeSales(Sales(itemName = it))
                actionItemNameInc.invoke()
            },
            label = { Text("Item name")}
        )

        Spacer(modifier = Modifier.height(16.dp))

        OutlinedTextField(
            value = roundQuantity(sales.quantitySold),
            textStyle = TextStyle(color = Color.DarkGray),
            onValueChange = {
                actionChangeSales(Sales(quantitySold = getDoubleFromEditText(it)))
                actionQuantityInc.invoke()
            },
            label = { Text("Quantity")},
            modifier = Modifier.width(120.dp)
        )
    }
}

在此处输入图像描述

As a workaround, try to wrap OutlinedTextField with Box and apply the width modifier there:作为一种解决方法,尝试用Box包装OutlinedTextField并在那里应用宽度修饰符:

            Box(modifier = Modifier.width(120.dp)) {
                OutlinedTextField(
                    value = "123456789",
                    textStyle = TextStyle(color = Color.DarkGray),
                    onValueChange = {},
                    label = { Text("Quantity") },
                )
            }

You have to wrap them in a Column again.您必须再次将它们包装在Column中。

Pseudo code as below伪代码如下

Column(
    horizontalAlignment = Alignment.CenterHorizontally,
    modifier = Modifiler.fillMaxWidth() // needed so it knows the full width to center the content within it
) {
    // This wraps your content and centers it
    
    Column( horizontalAlignment = Alignment.Start ){
        // This wraps and aligns the content to the left/start

        // place your content here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM