Центральное расположение сетки в линейном расположении - PullRequest
0 голосов
/ 25 марта 2019

У меня есть Gridlayout внутри Linearlayout, и я хочу, чтобы Gridlayout был горизонтально центрирован (т. Е. На одинаковом расстоянии слева и справа):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/outerLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10pt"
    android:paddingRight="10pt"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">

    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:columnWidth="200px"
        android:horizontalSpacing="0dp"
        android:numColumns="auto_fit"
        android:stretchMode="none"
        android:verticalSpacing="0dp" />

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start..."/>
</LinearLayout>

Gridlayout центрирован, но, кажется, непоэтому, поскольку он не плотно упаковывает содержимое (в правой части Gridlayout все еще есть «пустое пространство»), даже если установлено значение layout_width="wrap_content".

1 Ответ

0 голосов
/ 25 марта 2019

вам нужно использовать stretchMode в качестве равномерной ширины интервала вместо none . Установите гравитацию как центр .

В StretchMode есть еще две опции. Проверьте эту ссылку

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:columnWidth="200px"
    android:gravity="center"
    android:horizontalSpacing="0dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="0dp" />

enter image description here

...