I'm trying to create a layout composition using ConstraintLayout. In order to simplify my case, my layout should have three parts:
The part I'm struggling with is setting a max height for the first layout (red). It seems like the ConstraintLayout ignores my "max height statements":
app:layout_constraintHeight_max="300dp"
Here's my current result (the red part ignores the height limit..):
Here's the full XML:
<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:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" tools:context="com.mixtiles.android.reviewOrder.ReviewOrderActivity" tools:layout_editor_absoluteY="25dp"> <android.support.design.widget.AppBarLayout android:id="@+id/appBarLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <android.support.v7.widget.Toolbar android:id="@+id/review_order_toolbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> </android.support.v7.widget.Toolbar> </android.support.design.widget.AppBarLayout> <FrameLayout android:id="@+id/red" android:layout_width="0dp" android:layout_height="0dp" android:background="@color/red" app:layout_constraintBottom_toTopOf="@+id/green" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHeight_max="300dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/appBarLayout" app:layout_constraintVertical_chainStyle="spread_inside"> </FrameLayout> <FrameLayout android:id="@+id/green" android:layout_width="0dp" android:layout_height="150dp" android:background="@color/greenish" app:layout_constraintBottom_toTopOf="@+id/pink" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/red"> </FrameLayout> <FrameLayout android:id="@+id/pink" android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/pink" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/green"> </FrameLayout>
Following are the differences/advantages: Constraint Layout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout).
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.
Layout with 2 views on different sides. 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.
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).
android:layout_height="wrap_content" app:layout_constraintHeight_max="300dp" app:layout_constrainedHeight="true"
be sure to set the height wrap_content
<View android:id="@+id/view2" android:layout_width="88dp" android:layout_height="0dp" android:background="#F44" app:layout_constraintBottom_toTopOf="@+id/view" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" app:layout_constraintVertical_chainStyle="packed" app:layout_constraintHeight_max="300dp" /> <View android:id="@+id/view" android:layout_width="88dp" android:layout_height="150dp" android:background="#690" app:layout_constraintBottom_toTopOf="@+id/view3" app:layout_constraintTop_toBottomOf="@+id/view2" /> <View android:id="@+id/view3" android:layout_width="88dp" android:layout_height="150dp" android:background="#93C" app:layout_constraintBottom_toBottomOf="parent" />
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