LinearLayout儿童权重

时间:2018-02-07 02:35:42

标签: android android-layout android-linearlayout android-layout-weight

我想要一个LinearLayout,其权重包含另一个带权重的LinearLayout。问题是,当我尝试为子布局设置权重时,它会在为LinearLayout子设置权重时发出关于性能不佳的警告。我基本上想要2个线性布局,50%(0.5重量)包含另一个LinearLayout分为4个布局,所以网格有2列和2行。

2 个答案:

答案 0 :(得分:1)

我会用ConstraintLayout来做这件事。 ConstraintLayout具有PercentRelativeLayoutRelative 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>

enter image description here

答案 1 :(得分:0)

试试PercentRelativeLayoutPercentRelativeLayoutLinearLayout更灵活。