dor506
dor506

Reputation: 5414

ConstraintLayout - constraintHeight_percent gets ignored when view above another view?

]I'm using constraint layout in version 1.1.0 beta 6 (Which is way better than the stable 1.0.2 by the way...)

However, using constraintHeight_percent doesn't work for me in some case.

Here's what I want to achieve:

  1. Place a button(bottom-button) with a fixed height at the bottom of the constraint layout
  2. Place another button(top-button) between the top of the constraint layout, and above bottom-button.
  3. Make the top-button final height to be half of its height.

Here's the xml:

<?xml version="1.0" encoding="utf-8"?>
<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="300dp"
    android:background="#71B9DE">

    <Button
        android:id="@+id/bottomButton"
        android:layout_width="0dp"
        android:layout_height="150dp"
        android:text="BOTTOM BUTTON"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="TOP BUTTON - The height should be 75dp!!"
        app:layout_constraintBottom_toTopOf="@id/bottomButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHeight_percent="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Here's the preview:

enter image description here

And here's how I expected it to be:

enter image description here

I don't understand why after using app:layout_constraintHeight_percent="0.5", the height of the top button is still 150DP and not 75DP.

It seems like app:layout_constraintHeight_percent="0.5" calculation is done related to the parent height, and not related to the button height itself, after it got constrained.

Btw, I'm not looking for other solutions (Using guidelines, barriers). I really try to figure out why it doesn't work.

Thanks for the help!

Upvotes: 1

Views: 2324

Answers (2)

Pawel Laskowski
Pawel Laskowski

Reputation: 6346

According to official reference document in the section 'MATCH_CONSTRAINT dimensions (Added in 1.1)':

layout_constraintWidth_percent and layout_constraintHeight_percent : will set the size of this dimension as a percentage of the parent

provided that the corresponding constraints are set to MATCH_CONSTRAINT (0dp)

Upvotes: 3

Storg
Storg

Reputation: 33

You shoud use android:layout_width="match_parent" instead of 0dp

Upvotes: -2

Related Questions