Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout doesn't respect max width/height in combination with DimensionRatio

The view below needs to be square. I also want to the view to be restricted to a certain size. Somehow the ConstraintLayout is ignoring the max width/height property when this is used in combination with dimensionRatio on the same element.

<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">
    <View 
       android:layout_width="0dp"
       android:layout_height="0dp"
       app:layout_constraintTop_toTopOf="@+id/guideline_top"
       app:layout_constraintDimensionRatio="1:1"
       app:layout_constraintLeft_toLeftOf="@+id/guideline_left"
       app:layout_constraintRight_toRightOf="@+id/guideline_right"
       app:layout_constraintBottom_toBottomOf="@+id/guideline_bottom" 
       app:layout_constraintWidth_max="100dp"
       app:layout_constraintHeight_max="100dp"/>

At the moment I'm using a workaround by putting the View in a container as shown below. But I'd rather keep my layout-hierarchy as flat as possible.

<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.ConstraintLayout 
          android:layout_width="0dp"
          android:layout_height="0dp"
          app:layout_constraintTop_toTopOf="@+id/guideline_top"
          app:layout_constraintLeft_toLeftOf="@+id/guideline_left"
          app:layout_constraintRight_toRightOf="@+id/guideline_right"
          app:layout_constraintBottom_toBottomOf="@+id/guideline_bottom"
          app:layout_constraintWidth_max="100dp"
          app:layout_constraintHeight_max="100dp"> 
             <View 
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintDimensionRatio="1:1"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent" 
                />

Is this is a restriction in the constraintlayout that you can't use max width/height in combination with dimension ratio?

I'm using ConstraintLayout v1.0.2.

like image 810
Jono0 Avatar asked Sep 21 '17 09:09

Jono0


People also ask

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout. However, learning the basics of LinearLayout and RelativeLayout is important before trying to understand how to use it with ConstraintLayout.

Is ConstraintLayout a ViewGroup?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

What is ConstraintLayout chain?

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups).


1 Answers

Specify either

`app:layout_constraintDimensionRatio="H,1:1"` 

or

`app:layout_constraintDimensionRatio="W,1:1"`

This should causeConstraintLayout pick up your max width and height. See "Ratios" in the doc.

like image 177
Cheticamp Avatar answered Sep 28 '22 00:09

Cheticamp