ANDROID:ConstraintLayout:layout_constraintVertical_bias未按预期工作

时间:2017-05-25 09:42:54

标签: android android-constraintlayout

所以我正在尝试了解新的ConstraintLayout内容,因为iOS开发人员似乎没事。

这是我的观点enter image description here

我要做的是让4个方格在垂直轴上的尺寸略有不同(因此粉红色为60%,蓝色低于40%,蓝色位于右侧40%,黑色为60%)

阅读layout_constraintVertical_bias,这应该做我想要的,但似乎什么都不做

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

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_guideline"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="58dp"
        app:layout_constraintGuide_percent="0.16111112" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_guideline"
        app:layout_constraintGuide_percent="0.85"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="306dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_guideline"
        app:layout_constraintGuide_percent="0.85"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="434dp"
        tools:layout_editor_absoluteX="0dp" />

    <ImageButton
        android:id="@+id/yourStoryButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
        app:layout_constraintRight_toLeftOf="@+id/right_guideline"
        app:layout_constraintTop_toTopOf="@+id/bottom_guideline"
        app:layout_constraintVertical_bias="0.0" />

    <ImageButton
        style="?android:attr/borderlessButtonStyle"
        android:padding="0dp"
        android:id="@+id/leftTopButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/leftBottomButton"
        app:layout_constraintLeft_toRightOf="@+id/left_main_guideline"
        app:layout_constraintRight_toLeftOf="@+id/rightTopButton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.6"
        app:srcCompat="@color/colorAccent" />

    <ImageButton
        style="?android:attr/borderlessButtonStyle"
        android:padding="0dp"
        android:id="@+id/rightTopButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/rightBottomButton"
        app:layout_constraintLeft_toRightOf="@+id/leftTopButton"
        app:layout_constraintRight_toLeftOf="@+id/right_main_guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.4"
        app:srcCompat="@color/colorPrimary" />

    <ImageButton
        style="?android:attr/borderlessButtonStyle"
        android:padding="0dp"
        android:id="@+id/leftBottomButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_guideline"
        app:layout_constraintTop_toBottomOf="@+id/leftTopButton"
        app:layout_constraintLeft_toRightOf="@+id/left_main_guideline"
        app:layout_constraintRight_toLeftOf="@+id/rightBottomButton"
        app:layout_constraintVertical_bias="0.4"
        app:srcCompat="@color/colorPrimaryDark" />

    <ImageButton
        android:id="@+id/rightBottomButton"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_guideline"
        app:layout_constraintLeft_toRightOf="@+id/leftBottomButton"
        app:layout_constraintRight_toLeftOf="@+id/right_main_guideline"
        app:layout_constraintTop_toBottomOf="@+id/rightTopButton"
        app:layout_constraintVertical_bias="0.6"

        app:srcCompat="@android:color/background_dark" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_main_guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.0"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_main_guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="1.0"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="360dp" />

</android.support.constraint.ConstraintLayout>

我不正确app:layout_constraintVertical_bias控制该视图的增长百分比(如重量确实)

3 个答案:

答案 0 :(得分:4)

而不是设置layout_constraintVertical_bias设置此layout_constraintVertical_weight

答案 1 :(得分:2)

所以,我仍然对layout_constraintVertical_bias应该做什么感到困惑,不过app:layout_constraintVertical_weight做了我期望layout_constraintVertical_bias做的事情

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

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_guideline"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="58dp"
        app:layout_constraintGuide_percent="0.16111112" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_guideline"
        app:layout_constraintGuide_percent="0.85"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="306dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bottom_guideline"
        app:layout_constraintGuide_percent="0.85"
        android:orientation="horizontal"
        tools:layout_editor_absoluteY="434dp"
        tools:layout_editor_absoluteX="0dp" />

    <ImageButton
        android:id="@+id/yourStoryButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
        app:layout_constraintRight_toLeftOf="@+id/right_guideline"
        app:layout_constraintTop_toTopOf="@+id/bottom_guideline"
        app:layout_constraintVertical_bias="0.0" />

    <ImageButton
        style="?android:attr/borderlessButtonStyle"
        android:padding="0dp"
        android:id="@+id/leftTopButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/leftBottomButton"
        app:layout_constraintLeft_toRightOf="@+id/left_main_guideline"
        app:layout_constraintRight_toLeftOf="@+id/rightTopButton"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="0.6"
        app:srcCompat="@color/colorAccent" />

    <ImageButton
        android:id="@+id/rightTopButton"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="0dp"
        app:layout_constraintBottom_toTopOf="@+id/rightBottomButton"
        app:layout_constraintLeft_toRightOf="@+id/leftTopButton"
        app:layout_constraintRight_toLeftOf="@+id/right_main_guideline"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="0.4"
        app:srcCompat="@android:color/holo_orange_dark" />

    <ImageButton
        style="?android:attr/borderlessButtonStyle"
        android:padding="0dp"
        android:id="@+id/leftBottomButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_guideline"
        app:layout_constraintTop_toBottomOf="@+id/leftTopButton"
        app:layout_constraintLeft_toRightOf="@+id/left_main_guideline"
        app:layout_constraintRight_toLeftOf="@+id/rightBottomButton"
        app:layout_constraintVertical_weight="0.4"
        app:srcCompat="@color/colorPrimaryDark" />

    <ImageButton
        android:id="@+id/rightBottomButton"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:padding="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_guideline"
        app:layout_constraintLeft_toRightOf="@+id/leftBottomButton"
        app:layout_constraintRight_toLeftOf="@+id/right_main_guideline"
        app:layout_constraintTop_toBottomOf="@+id/rightTopButton"
        app:layout_constraintVertical_weight="0.6"
        app:srcCompat="@android:color/background_dark" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_main_guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.0"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_main_guideline"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="1.0"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="360dp" />

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:1)

要使其正常运行,您还应该指定chainStyle

app:layout_constraintVertical_chainStyle="packed"