简体   繁体   English

Jetpack Compose 中约束布局的权重

[英]Weights for Constraint Layout in Jetpack Compose

Is there any way to use weights in Jetpack Compose ConstraintLayout chains like in XML:有什么方法可以在Jetpack Compose ConstraintLayout 链中使用权重,例如 XML:

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:id="@+id/first"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#caf"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/start"
        app:layout_constraintHorizontal_weight="6"/>

    <View
        android:id="@+id/second"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:background="#fac"
        app:layout_constraintLeft_toRightOf="@+id/first"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_weight="4"/>

</android.support.constraint.ConstraintLayout>

Currently, neither ConstraintLayoutScope or ConstraintScope provide weight functionality.目前, ConstraintLayoutScopeConstraintScope都不提供权重功能。 ConstraintLayoutScope provides createHorizontalChain(vararg elements: ConstraintLayoutReference, chainStyle: ChainStyle) (or createVerticalChain respectively) but without the option to set weights to individual elements. ConstraintLayoutScope提供createHorizontalChain(vararg elements: ConstraintLayoutReference, chainStyle: ChainStyle) (或分别为createVerticalChain ),但没有为单个元素设置权重的选项。

With guidelines, barriers and everything else integrated I feel like there's something important missing here.有了指南、障碍和其他所有东西,我觉得这里缺少一些重要的东西。 Is there a way to use weights in chains or emulate their behaviour?有没有办法在链中使用权重或模仿它们的行为? I cannot just specify a fixed width for a element since the width of the ConstraintLayout has to match the whole screen.我不能只为元素指定一个固定宽度,因为 ConstraintLayout 的宽度必须与整个屏幕匹配。

Note: I understand that the new Jetpack Compose ConstraintLayout does not have a performance advantage for complex, otherwise nested, layout structures like the Android View ConstraintLayout had.注意:我知道新的 Jetpack Compose ConstraintLayout 对于复杂的嵌套布局结构(例如 Android View ConstraintLayout)没有性能优势。 I'm also aware that Row and Column provide weight functionality but I have to use ConstraintLayout in my case ( reason )我也知道 Row 和 Column 提供权重功能,但我必须使用 ConstraintLayout 在我的情况下( 原因

Have you tried fillMaxWidth(faction) modifier?您是否尝试过fillMaxWidth(faction)修饰符? I did this and worked for me...我这样做并为我工作......

@Composable
fun ConstraintLayoutWeightDemo() {
    ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
        val (refB1, refB2, refB3) = createRefs()
        Text(
            "Text 1",
            modifier = Modifier
                .constrainAs(refB1) {
                    start.linkTo(parent.start)
                    end.linkTo(refB2.start)
                }
                .fillMaxWidth(.3f) // <<<---- 30%
                .background(Color.Red)
        )
        Text(
            "Text 2",
            modifier = Modifier
                .constrainAs(refB2) {
                    start.linkTo(refB1.end)
                    end.linkTo(refB3.start)
                }
                .fillMaxWidth(.5f) // <<<---- 50%
                .background(Color.Green)
        )
        Text(
            "Text 3",
            modifier = Modifier
                .constrainAs(refB3) {
                    start.linkTo(refB2.end)
                    end.linkTo(parent.end)
                }
                .fillMaxWidth(.2f) // <<<---- 20%
                .background(Color.Blue)
        )
    }
}

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

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