Android GridLayout по горизонтали
Я пытаюсь создать GridLayout с двумя столбцами, которые будут центрированы.
Мой авторский дизайн:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:gravity="top|left"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:gravity="top|left"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
И это выглядит так:
![FAOq0.png]()
И я хотел бы иметь эти кнопки в центре и идеально с интервалом между ними.
Возможно ли это?
- EDIT:
Я также попытался поместить его в LinearLayout без результатов:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<GridLayout xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
custom:rowCount="4"
custom:columnCount="2"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</LinearLayout>
Ответы
Ответ 1
Сделайте сетку обтеканием ее содержимого горизонтально с помощью layout_width="wrap_content"
и установите ее layout_gravity
на center
:
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
// ......
>
Ответ 2
Из документации GridLayout:
GridLayout распределение избыточного пространства основано на приоритете, а не на весе.
(...)
Чтобы растянуть столбец, убедитесь, что все компоненты внутри него определяют гравитацию.
Поэтому, очевидно, вам нужно установить layout_gravity
на android:layout_gravity="top|center"
(я не проверял это, но из документации это должно быть в том же духе).
Ответ 3
Ты почти там. Я думаю, что это сработает:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:rowCount="4"
custom:columnCount="2"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="green"
custom:layout_row="0"
custom:layout_column="0" />
<TimeTableKeeper.Tile
android:layout_width="75dp"
android:layout_height="75dp"
android:background="#00FF00"
custom:color="blue"
custom:layout_row="0"
custom:layout_column="1" />
</GridLayout>
</FrameLayout>
Ответ 4
Код ниже решил мою проблему.
<RelativeLayout 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">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:columnCount="3"
android:rowCount="4"
android:useDefaultMargins="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</GridLayout>
</RelativeLayout>
Ответ 5
скопируйте этот пример для раскладки калькулятора =)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<!-- android:useDefaultMargins="true" (OPTIONAL) -->
<GridLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:useDefaultMargins="true"
>
<Button
android:layout_column="0"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="/"/>
<Button
android:layout_column="1"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"/>
<Button
android:layout_column="2"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"/>
<Button
android:layout_column="3"
android:layout_row="0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"/>
<Button
android:layout_column="0"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="*"/>
<Button
android:layout_column="1"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"/>
<Button
android:layout_column="2"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"/>
<Button
android:layout_column="3"
android:layout_row="1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"/>
<Button
android:layout_column="0"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="-"/>
<Button
android:layout_column="1"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"/>
<Button
android:layout_column="2"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"/>
<Button
android:layout_column="3"
android:layout_row="2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"/>
<Button
android:layout_column="0"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="+"/>
<Button
android:layout_column="1"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="0"/>
<Button
android:layout_column="2"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="00"/>
<Button
android:layout_column="3"
android:layout_row="3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="="/>
</GridLayout>
</LinearLayout>