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

XML:
<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>发布于 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不是其中之一。
发布于 2015-03-24 13:43:11
您可以使用gridview属性numcolumns。
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"/>发布于 2015-03-24 13:46:11
您可以尝试下面的代码,它适用于我:
<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 :在其父视图中设置视图或布局的重力。
https://stackoverflow.com/questions/29234116
复制相似问题