1

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

这是供参考的布局:

<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。我怎样才能做到这一点?

4

2 回答 2

4

如果您在视图中使用水平链,则水平偏置不起作用(因为您在同一轴上提供偏置,如果存在水平链,则垂直偏置将起作用,反之亦然);除非你想将它应用到你创建的链的头视图(这是水平链中最左边的视图和垂直链中最顶部的视图);这不是你的情况,在这里Also, the applied biasing on the head view of the chain only works if the selected chain style is packed. 因此,您应该尝试找到一些其他解决方法来实现您想要的 UI 并忽略使用 chain (here)

有关更多信息,请参阅:使用 ConstraintLayout 构建响应式 UI

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

于 2018-09-26T14:27:31.130 回答
2

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

 <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,但在链内部这不起作用。

于 2018-09-26T14:44:16.510 回答