Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID : ConstraintLayout : layout_constraintVertical_bias not working as expected

So I am trying to learn about the new ConstraintLayout stuff, as an iOS developer things seem ok.

This is my view enter image description here

What I am trying to do is to get the 4 squares to be slightly different sizes on the vertical axis (so pink 60%, blue below 40%, blue on the right 40%, black 60%)

Reading up on layout_constraintVertical_bias, this should do what I want however it seems to do nothing

<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>

Am I incorrect that app:layout_constraintVertical_bias controls the growth percentage of that view (like weight did)

like image 848
Burf2000 Avatar asked May 25 '17 09:05

Burf2000


3 Answers

Rather than setting layout_constraintVertical_bias set this layout_constraintVertical_weight

like image 137
schinj Avatar answered Oct 21 '22 17:10

schinj


To make it work you should also specify chainStyle

app:layout_constraintVertical_chainStyle="packed"
like image 28
Maksym V. Avatar answered Oct 21 '22 16:10

Maksym V.


So, I am still baffled by what layout_constraintVertical_bias should do, however app:layout_constraintVertical_weight did what I expected layout_constraintVertical_bias to do

<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>
like image 29
Burf2000 Avatar answered Oct 21 '22 16:10

Burf2000