Is this possible?
I want following:
- HEADER of a defined height (e.g. 64dp)
- 4 rows of evenly distributed heights (25% of view height MINUS header height <= this is the problematic detail)
Can I somehow achieve this with a ConstraintLayout
?
Problem
I want to add a header of a fixed height and I want the the percentage constraints do not calculate their percentages based on the ConstraintLayout
s height but on the remaining space inside the ConstraintLayout
after substracting the header.
Important Note
Yes, I can wrap the whole ConstraintLayout
inside a LinearLayout
but I'm interested in a ConstraintLayout
only based solution.
Edit 1
To make it more clear, I want following:
h_full... full height of ConstraintLayout
h_header... full height of header
Vertical arrangement of views should look like following:
- HEADER - y = 0
- VIEW 1 - y = h_header
- VIEW 2 - y = h_header + (h_full - h_header) / 4
- VIEW 3 - y = h_header + (h_full - h_header) / 4 * 2
- VIEW 4 - y = h_header + (h_full - h_header) / 4 * 3
Let's assume following:
h_full = 1000
h_header = 100
space for views = 1000 - 100 = 900 (this is the base for the percentages!)
h_view = 900 / 4 = 225
Then we get following:
- HEADER - y = 0
- VIEW 1 - y = 100
- VIEW 2 - y = 100 + 225
- VIEW 3 - y = 100 + 225 * 2
- VIEW 4 - y = 100 + 225 * 3
Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<!-- Guidelines -->
<!-- 3 horizontal guidelines -->
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_horizontal1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_horizontal2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_horizontal3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75" />
<!-- Views - HEADER -->
<View
android:id="@+id/header"
android:layout_width="0dp"
android:layout_height="64dp"
android:layout_marginLeft="@dimen/calender_cell_padding"
android:layout_marginTop="@dimen/calender_cell_padding"
android:layout_marginRight="@dimen/calender_cell_padding"
android:layout_marginBottom="@dimen/calender_cell_padding"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- Views - 4 Views, evenly distrubuted vertically -->
<View
android:id="@+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/guideline_horizontal1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/guideline_horizontal2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline_horizontal1" />
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/guideline_horizontal3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline_horizontal2" />
<View
android:id="@+id/view4"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/guideline_horizontal3" />
</androidx.constraintlayout.widget.ConstraintLayout>