-1

I want to have a LinearLayout with a weight containing another LinearLayout with weight. The problem is, when I try to set weight for children layouts, it gives a warning about bad performance when setting weights to LinearLayout children. I basically want 2 Linear Layouts, both 50% (0.5 weight) containing another LinearLayout divided into 4 Layouts, so a grid with 2 columns and 2 rows.

THZ
  • 155
  • 1
  • 9

2 Answers2

1

I would use ConstraintLayout to do this. ConstraintLayout has all the best features of both PercentRelativeLayout and Relative Layout, and is currently under active improvement by 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

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Ben P.
  • 52,661
  • 6
  • 95
  • 123
0

Try PercentRelativeLayout. PercentRelativeLayout is more flexible than LinearLayout.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
leimenghao
  • 226
  • 1
  • 10