Android:无法将我的GridLayout居中
我想显示一个由15个按钮、3列和5行组成的网格。 它们应该水平和垂直居中,在同一方向上它们之间的间距相等(意味着所有按钮之间的水平间距相同,但只要垂直间距相同,就可以在垂直方向上有另一个间距) 下面是它现在的样子(根本不是我想要的):Android:无法将我的GridLayout居中,android,android-gridlayout,Android,Android Gridlayout,我想显示一个由15个按钮、3列和5行组成的网格。 它们应该水平和垂直居中,在同一方向上它们之间的间距相等(意味着所有按钮之间的水平间距相同,但只要垂直间距相同,就可以在垂直方向上有另一个间距) 下面是它现在的样子(根本不是我想要的): 如果您使用相对布局并对代码做了一些更改,则可以居中显示,下面是我的: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:la
如果您使用相对布局并对代码做了一些更改,则可以居中显示,下面是我的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#ffff00"
android:orientation="vertical" >
<GridLayout
android:id="@+id/choose_level_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="5"
android:horizontalSpacing="60dp"
android:layout_centerInParent="true"
android:verticalSpacing="60dp" >
在RelativeLayout和GridLayout上设置此属性后,结果如下:
Ty,这很有效。但是,我也希望按钮之间的间距均匀。我怎样才能做到这一点?是的,你只需要给每个按钮添加边距:安卓:layout\u margin=“20dp”Ty,这是合理的。但是如果有意义的话,我希望按钮之间的边距与外部按钮之间的边距和视图侧面的边距相同。是的,我知道了。我的建议是以编程方式调整边距,因为您可能会在具有不同屏幕大小和密度的不同设备上运行此应用程序。您只需要问另一个问题或寻找以编程方式设置页边距的良好做法。由于我帮助您解决了原始问题,使gridview的内容居中,您能否将我的答案标记为已接受?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#ffff00"
android:orientation="vertical" >
<GridLayout
android:id="@+id/choose_level_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="5"
android:horizontalSpacing="60dp"
android:layout_centerInParent="true"
android:verticalSpacing="60dp" >