Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout aspect ratio

Consider the following layout file:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout     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:id="@+id/activity_main"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:background="#FF0000"         android:paddingBottom="@dimen/activity_vertical_margin"         android:paddingLeft="@dimen/activity_horizontal_margin"         android:paddingRight="@dimen/activity_horizontal_margin"         android:paddingTop="@dimen/activity_vertical_margin">          <ImageView             android:layout_width="0dp"             android:layout_height="0dp"             android:background="#0000FF"             android:padding="16dp"             app:layout_constraintBottom_toBottomOf="parent"             app:layout_constraintTop_toTopOf="parent"             app:layout_constraintLeft_toLeftOf="parent"             app:layout_constraintDimensionRatio="H,3:1"             tools:layout_editor_absoluteX="16dp" />      </android.support.constraint.ConstraintLayout>  </RelativeLayout> 

I am not sure how the app:layout_constraintDimensionRatio works. My understanding is the ratio will always be width:height. So 3:1 will always make the ImageView appear 3 times wider than height. The prefix H or W tells ConstraintLayout which dimension should respect the ratio. If it is H then it means width will be first computed from other constraints and then height will be adjusted according to the aspect ratio. However this is the result of the layout:

enter image description here

The height is 3 times larger than width which is unexpected. Can anyone explain to me how the dimensions are computed with respect to app:layout_constraintDimensionRatio setting?

like image 804
darklord Avatar asked Dec 21 '16 14:12

darklord


People also ask

How do you set a ratio in ConstraintLayout?

If you set app:layout_constraintDimensionRatio="H,3:1" then it means width will be first computed from other constraints and then height will be adjusted according to the aspect ratio.

Which is better RelativeLayout or ConstraintLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.

Is ConstraintLayout faster than LinearLayout?

Results show that the fastest layout is Relative Layout, but difference between this and Linear Layout is really small, what we can't say about Constraint Layout. More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.

What is layout_constraintDimensionRatio?

A ConstraintLayout is a ViewGroup which allows you to Position and size Views in a flexible way. It is basically RelativeLayout on Steriods. The Views will be positioned and sized w.r.t other Views with the Use of Constraints.


1 Answers

Your understanding for the way app:layout_constraintDimensionRatio works is correct. If you set app:layout_constraintDimensionRatio="H,3:1" then it means width will be first computed from other constraints and then height will be adjusted according to the aspect ratio. The only problem with your implementation is that you added app:layout_constraintBottom_toBottomOf="parent" to the ImageView, so that it caused app:layout_constraintDimensionRatio to be ignored.

Here's the layout to size your ImageView in 3:1 aspect ratio:

<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="match_parent"     android:background="#FF0000">      <ImageView         android:id="@+id/imageView"         android:layout_width="0dp"         android:layout_height="0dp"         android:layout_marginStart="16dp"         android:layout_marginTop="16dp"         android:layout_marginEnd="16dp"         android:background="#0000FF"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintDimensionRatio="H,3:1" />  </android.support.constraint.ConstraintLayout> 

and here's the result view:

ImageView in 3:1 aspect ratio

like image 200
Eugene Brusov Avatar answered Oct 14 '22 18:10

Eugene Brusov