I would like to create this simple layout:
I wrote this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/purple_200">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:background="@color/black" />
</LinearLayout>
But doing so I don't see any black view appearing. What am I missing?
CodePudding user response:
There is a new way called ConstraintLayout Which is prefferred. You can use layout_constraintHeight_percent property of it.
Example:
<?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"
android:background="@color/purple_200"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
CodePudding user response:
You have to add
android:weightSum="1"
to your parent's layout.
I suppose you thought it's 1 by default, but it's not, you have to specify it.
From the android:weightSum definition:
Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.
CodePudding user response:
Checkout Layout weight:
https://developer.android.com/guide/topics/ui/layout/linear#Weight
Add a second Layout to set in relation to the first one.


