我正在创建一个想要在主屏幕上显示3种布局的应用程序。 1在顶部,2在底部。它的比例应为2:3:3,顶部:中间:底部。但是由于某种原因,我无法使它们保持适当的重量。请帮忙。
<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>
输出必须具有顶部布局,顶部布局,中间布局和底部布局。布局的位置是正确的,但是我似乎无法以我希望的比例获得尺寸调整信息。
答案 0 :(得分:1)
使用app:layout_constraintVertical_weight
设置权重时,仅在父级为ConstraintLayout
时适用。解决此问题的一个简单方法是使用LinearLayout作为根父对象,然后将高度设置为0,并相应地设置权重。
您也可以将ConstraintLayout
作为根并设置权重,但是在这种情况下,如果根是LinearLayout
,我认为这很简单。
<?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>