Я хочу достичь этого макета:
Ближайшим, к которому я смог прийти, является следующее:
Макет до сих пор таков:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:layout_centerVertical="true" android:baselineAligned="true" android:orientation="vertical" android:weightSum="4" > <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_weight="1" android:paddingBottom="8dip" > <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_weight="1" android:paddingBottom="8dip" > <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_weight="1" android:paddingBottom="8dip" > <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_weight="1" android:paddingBottom="8dip" > <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> <ImageButton android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:scaleType="fitCenter" android:src="@drawable/myimage" /> </LinearLayout> </LinearLayout>
Как создать схему выше?
Этот макет действительно работает в соответствии с вашими ожиданиями:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- Center layout --> <LinearLayout android:id="@+id/llCenter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_centerInParent="true" android:paddingBottom="8dp" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> </LinearLayout> <!-- Left layout --> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/llCenter" android:paddingBottom="8dp" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> </LinearLayout> <!-- Right layout --> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/llCenter" android:paddingBottom="8dp" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> </LinearLayout> </RelativeLayout>
Результат:
Создайте три линейных выхода с ориентацией, установленной на вертикальные и равные макеты. Дочерние представления каждого линейного макета должны также иметь равные макеты. Установите гравитацию первого и третьего линейных выходов как центрально-вертикальные.
Почему бы вам не оставить ненужные макеты … ?? И используйте следующие …
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="100dp" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="100dp" android:layout_height="55dp" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="75dp" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="95dp" android:orientation="vertical" > </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="100dp" android:layout_height="55dp" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="75dp" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="95dp" android:orientation="vertical" > </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="100dp" android:layout_height="55dp" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="75dp" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="100dp" android:layout_height="95dp" android:orientation="vertical" > </LinearLayout> </LinearLayout> </LinearLayout>
Как насчет расширения ViewGroup ?
Если вы проверите документацию, появится пример того, как создать пользовательский контейнер для детей.
В основном в вашем макете есть следующие правила:
Если вы в состоянии сделать это с помощью специального макета, за счет большего времени на разработку, у вас будет лучший результат, и не имеет значения количество элементов, которые вы хотите добавить … у вас будет Макет, который может управлять разными ситуациями.
Я также предлагаю вам ознакомиться с настраиваемыми атрибутами , чтобы обеспечить интеграцию атрибутов, которые вам понадобятся непосредственно в декларации XML.