layout 안의 뷰 여러 개를 percent에 따라 화면의 각 비율만큼 차지하게 하고 싶을 때 부모 layout에 따라 값을 지정하는 방법이 다르다.
Linearlayout과 Constraintlayout의 두 가지 경우를 살펴보자.
두 경우 모두 weight을 지정하려는 자식 view의 width나 height 값을 0dp로 설정해야 한다. 아래 코드에서는 layout_height 값을 0dp로 하고 비율을 9 : 1로 설정했다.
1. 부모가 LinearLayout일 때는 부모에 weightSum을 설정하고 자식에 layout_weight 값을 지정한다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:weightSum="10"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_navigation_menu" />
</LinearLayout>
2. 부모가 Constraintlayout일 때는 자식의 layout_constraintHeight_percent값을 지정한다. width일 때도 마찬가지. percent 값은 소수점으로 입력한다. (10% = 0.1로 입력)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
app:labelVisibilityMode="unlabeled"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frame_layout"
app:menu="@menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>