首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用GridView“android:layout_gravity=”对android:layout_gravity=项目进行居中

无法使用GridView“android:layout_gravity=”对android:layout_gravity=项目进行居中
EN

Stack Overflow用户
提问于 2015-03-24 13:37:03
回答 4查看 1K关注 0票数 0

我试图使用GridView在android:layout_gravity="center"中对项目进行居中,但是它们不断地被推到左边边缘,而且我似乎找不到我在这个实例中做错了什么。

XML:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ListView_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="1" >

    <RelativeLayout
        android:id="@+id/rl_ListView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/boxart" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_ListView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:layout_weight="0.5" >

        <GridLayout
            android:id="@+id/GridLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:columnCount="4"
            android:orientation="horizontal"
            android:rowCount="4" >

            <Button
                android:id="@+id/button0"
                android:layout_column="0"
                android:layout_row="0"
                android:text="Button" />

            <Button
                android:id="@+id/button1"
                android:layout_column="1"
                android:layout_row="0"
                android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_column="2"
                android:layout_row="0"
                android:text="Button" />
            <Button
                android:id="@+id/button3"
                android:layout_column="3"
                android:layout_row="0"
                android:text="Button" />
        </GridLayout>
    </RelativeLayout>

</LinearLayout>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-03-24 14:10:18

您正在RelativeLayout中使用您的RelativeLayout,因此android:layout_gravity="center"无法工作。

尝试将其替换为其中之一(作为您的中心首选项):

  • android:layout_centerHorizontal="true"
  • android:layout_centerVertical="true"
  • android:layout_centerInParent="true"

- 这里 -您可以找到RelativeLayout支持的所有布局参数。layout_gravity不是其中之一。

票数 1
EN

Stack Overflow用户

发布于 2015-03-24 13:43:11

您可以使用gridview属性numcolumns。

代码语言:javascript
复制
<GridView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"/>
票数 0
EN

Stack Overflow用户

发布于 2015-03-24 13:46:11

您可以尝试下面的代码,它适用于我:

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="5dip" >
<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="100dip"
    android:gravity="center"
    android:horizontalSpacing="10dip"
    android:numColumns="3"
    android:stretchMode="spacingWidthUniform"
    android:verticalSpacing="10dip" />
</LinearLayout>

值得注意的一个不同之处是:

android:重力:设置其使用的视图内容的重力。

android:layout_gravity :在其父视图中设置视图或布局的重力。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29234116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档