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.
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.
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).
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.
ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With