偏差属性被忽略

时间:2018-09-26 14:08:49

标签: android android-constraintlayout constraint-layout-chains

我有一个简单的水平链接视图布局,具有spread_inside链样式。当我尝试使用bias属性将视图移动到所需位置时,我发现bias属性被忽略了。

这里是供参考的布局:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <View
        android:id="@+id/view_1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@android:color/black"
        app:layout_constraintEnd_toStartOf="@id/view_2"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:background="@android:color/black"
        app:layout_constraintEnd_toStartOf="@id/view_3"
        app:layout_constraintStart_toEndOf="@id/view_1"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@android:color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        app:layout_constraintStart_toEndOf="@id/view_2"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

在这里,我想使用layout_constraintHorizo​​ntal_bias属性将view_3移近view_2。我该如何实现?

2 个答案:

答案 0 :(得分:1)

如果您在视图之间使用水平链,则

水平偏向不起作用(因为在同一轴上施加偏向,垂直偏向会如果有水平链则工作,反之亦然); 除了要在创建的链的头部视图(水平链中最左边的视图和垂直链中最顶部的视图)上应用的情况之外, ,这不是您的情况,此外,仅当压缩了选定的链样式时,才对链的头部视图施加偏见。因此,您应该尝试找到其他解决方法来实现所需的UI并忽略链的使用(在这里)

有关更多信息,请参见:Build a Responsive UI with ConstraintLayout

希望我的回答对您有所帮助。

答案 1 :(得分:1)

您可以像这样更改第一视图(链顶视图):

 <View
    android:id="@+id/view_1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@android:color/black"
    app:layout_constraintEnd_toStartOf="@+id/view_2"

    app:layout_constraintHorizontal_bias="0.1"
    app:layout_constraintHorizontal_chainStyle="packed"

    app:layout_constraintStart_toStartOf="parent" />

并通过layout_constraintHorizontal_bias设置相对于父级的所有链位置,但是在链内这不起作用。

相关问题