Reputation: 3
I am creating an app in which I want 3 layouts on the home screen. 1 at the top and 2 at the bottom. It should be in the ratio of 2:3:3 , top:middle:bottom. But for some reason I cannot make them weight properly. Please help.
<android.support.constraint.ConstraintLayout
android:id="@+id/quickMenu"
android:layout_width="match_parent"
app:layout_constraintVertical_chainStyle="spread_inside"
android:layout_height="0dp"
app:layout_constraintVertical_weight="2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent">
// some things go in here
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/topPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/quickMenu"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintVertical_weight="3">
// some buttons go in here
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/topPanel"
app:layout_constraintVertical_chainStyle="spread_inside"
app:layout_constraintVertical_weight="3">
//some more buttons go in here.
</android.support.constraint.ConstraintLayout>
The output has to be with the top layout at the top followed by the middle layout followed by the bottom layout. The position of the layouts are correct but i cant seem to get the sizing write in the ratio I want them to be.
Upvotes: 0
Views: 150
Reputation: 29280
When you set the weight using app:layout_constraintVertical_weight
, that applies only if the parent is a ConstraintLayout
. A simple solution to this problem is to use a LinearLayout as the root parent and then set the heights to 0 and the weights accordingly.
You could also have a ConstraintLayout
as the root and set weights, but I think it's simpler if the root is a LinearLayout
in this case.
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:id="@+id/quickMenu"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
// some things go in here
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/topPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
// some buttons go in here
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
//some more buttons go in here.
</android.support.constraint.ConstraintLayout>
</LinearLayout>
</layout>
Upvotes: 1