我想显示按钮网格15个按钮,3列和5行。 它们应该在水平和垂直方向上居中,它们之间的间距相同,方向相同(在所有按钮之间水平相同的空间,但只要垂直空间相同,就可以垂直放置另一个空间)。
这就是现在的样子(根本不是我想要的): http://sv.tinypic.com/view.php?pic=dbgjll&s=8#.U-ELkNbzTFY
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/choose_level_grid_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/wood_background"
android:columnCount="3"
android:horizontalSpacing="60dp"
android:verticalSpacing="60dp" >
<Button
android:id="@+id/level1_button"
android:layout_width="@dimen/choose_level_button_width"
android:layout_height="@dimen/choose_level_button_height"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:text="@string/level1_btn_text"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size" />
<Button
android:id="@+id/level2_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level2_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level3_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level3_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level4_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level4_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level5_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level5_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level6_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level6_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level7_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level7_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level8_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level8_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level9_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level9_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level10_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level10_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level11_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level11_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level12_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level12_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level13_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level13_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level14_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level14_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
<Button
android:id="@+id/level15_button"
android:background="@drawable/main_menu_button_shape"
android:padding="@dimen/main_menu_button_padding"
android:textColor="@color/textcolor_on_transparent_brown"
android:textSize="@dimen/choose_level_text_size"
android:text="@string/level15_btn_text"
android:layout_height="@dimen/choose_level_button_height"
android:layout_width="@dimen/choose_level_button_width"
/>
</GridLayout>
答案 0 :(得分:0)
如果您使用相对布局并对代码进行了一些更改,则可以居中,这是我的:
<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中设置此属性后,结果就是: