【问题标题】:How to align view in grid cells of GridLayout center?如何在 GridLayout 中心的网格单元格中对齐视图?
【发布时间】:2016-10-10 16:51:43
【问题描述】:

我有一个简单的 2x2 网格布局。其中 3 个具有红色、绿色和蓝色的视图。我希望它们在网格的单元格中居中对齐。我的布局如下,但即使我将所有视图的 layout_gravity 放在中心位置,视图也是左上角对齐的。我该如何解决这个问题?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.elyeproj.griddrag.MainActivity">

    <GridLayout
        android:id="@+id/container_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="2"
        android:orientation="horizontal">
        <View
            android:id="@+id/view_red"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_gravity="center"
            android:background="#ff0000" />

        <View
            android:id="@+id/view_green"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_gravity="center"
            android:background="#00ff00" />

        <View
            android:id="@+id/view_blue"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_gravity="center"
            android:background="#0000ff" />
    </GridLayout>
</RelativeLayout>

【问题讨论】:

    标签: android android-gridlayout


    【解决方案1】:

    网格布局项目是换行的,因此您最好使用权重根据需要正确对齐它们。 下面的xml就是你想要的:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    
    <GridLayout
        android:id="@+id/container_grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="2"
        android:rowCount="3"
        android:orientation="horizontal">
    
        <View
            android:id="@+id/view_red"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:layout_row="0"
            android:layout_column="0" />
    
        <View
            android:id="@+id/view_green"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_columnWeight="1"
            android:layout_gravity="center"
            android:background="#00ff00"
            android:layout_row="0"
            android:layout_column="1" />
    
        <View
            android:id="@+id/view_blue"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:layout_gravity="center"
            android:background="#0000ff"
            android:layout_row="1"
            android:layout_column="0" />
        </GridLayout>
    </RelativeLayout>
    

    请参阅下面的链接,了解使用 21 以下 api 的 linearlayout 的另一种解决方案: GridLayout (not GridView) how to stretch all children evenly

    【讨论】:

    • 太棒了!不幸的是,它只适用于 API21 :(。如果我希望它支持早期版本怎么办?
    • 是的,对于较低的 api,您必须在 java 代码中进行。只需计算 GridLayout 的大小,然后精确(按像素)设置视图大小,而不是使用权重;)如果有帮助,请接受答案
    • 21岁以下的更好解决方案请参见此页面:stackoverflow.com/questions/10016343/…
    • 谢谢。结帐该网站,找不到适合 21 岁之前的好解决方案。如果您提出了该解决方案,我肯定会将此作为答案。我赞成你的回答,因为它有帮助。
    • 看看我发给你的网址。它展示了一种使用线性布局的好方法,它非常简单。
    【解决方案2】:

    嘿,只需将这些行添加到每个视图。

    android:layout_columnWeight="1"
    android:layout_rowWeight="1"
    

    希望这会有所帮助。

    【讨论】:

    • 太棒了!不幸的是,它只适用于 API21 :(。如果我希望它支持早期版本怎么办?
    • 尝试在 API19 设备上运行它,检查它是否工作。我想它会起作用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2020-10-14
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多