【问题标题】:LinearLayout children weightsLinearLayout 子级权重
【发布时间】:2018-02-07 02:35:41
【问题描述】:
我想要一个具有权重的 LinearLayout,其中包含另一个具有权重的 LinearLayout。问题是,当我尝试为子布局设置权重时,它会在为 LinearLayout 子布局设置权重时给出性能不佳的警告。我基本上想要 2 个线性布局,两个 50%(0.5 权重)都包含另一个线性布局,分为 4 个布局,所以一个有 2 列和 2 行的网格。
【问题讨论】:
标签:
android
android-layout
android-linearlayout
android-layout-weight
【解决方案1】:
我会使用ConstraintLayout 来执行此操作。 ConstraintLayout 具有 PercentRelativeLayout 和 Relative Layout 的所有最佳功能,目前正在由 Google 积极改进。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<View
android:id="@+id/r1c1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#eee"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/r1c2"
app:layout_constraintBottom_toTopOf="@+id/r2c1"/>
<View
android:id="@+id/r1c2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ddd"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/r1c1"
app:layout_constraintRight_toLeftOf="@+id/r1c3"
app:layout_constraintBottom_toTopOf="@+id/r2c2"/>
<View
android:id="@+id/r1c3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#ccc"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/r1c2"
app:layout_constraintRight_toLeftOf="@+id/r1c4"
app:layout_constraintBottom_toTopOf="@+id/r2c3"/>
<View
android:id="@+id/r1c4"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#bbb"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@+id/r1c3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@+id/r2c4"/>
<View
android:id="@+id/r2c1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#aaa"
app:layout_constraintTop_toBottomOf="@+id/r1c1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/r2c2"
app:layout_constraintBottom_toBottomOf="parent"/>
<View
android:id="@+id/r2c2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#999"
app:layout_constraintTop_toBottomOf="@+id/r1c2"
app:layout_constraintLeft_toRightOf="@+id/r2c1"
app:layout_constraintRight_toLeftOf="@+id/r2c3"
app:layout_constraintBottom_toBottomOf="parent"/>
<View
android:id="@+id/r2c3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#888"
app:layout_constraintTop_toBottomOf="@+id/r1c3"
app:layout_constraintLeft_toRightOf="@+id/r2c2"
app:layout_constraintRight_toLeftOf="@+id/r2c4"
app:layout_constraintBottom_toBottomOf="parent"/>
<View
android:id="@+id/r2c4"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#777"
app:layout_constraintTop_toBottomOf="@+id/r1c4"
app:layout_constraintLeft_toRightOf="@+id/r2c3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>