Android 如何在GridLayout的相对父布局中居中?
Android 如何在GridLayout的相对父布局中居中?,android,css,Android,Css,... 诸如此类。。。 目前,gridlayout位于屏幕的最上方。我想把它水平和垂直居中。有什么想法吗 编辑:注意,centerInParent=“true”没有执行任何操作 EDIT2:找到了答案,我必须将gridlayout设置为包装内容,并且还必须编写android:layout\u centerVertical=“true”,因为您将高度和宽度设置为匹配\u parent而不是将内容垂直居中。将高度和宽度设置为wrap\u content 像这样: <RelativeLayou
... 诸如此类。。。
目前,gridlayout位于屏幕的最上方。我想把它水平和垂直居中。有什么想法吗
编辑:注意,centerInParent=“true”没有执行任何操作
EDIT2:找到了答案,我必须将gridlayout设置为包装内容,并且还必须编写android:layout\u centerVertical=“true”,因为您将高度和宽度设置为
匹配\u parent
而不是将内容垂直居中。将高度和宽度设置为wrap\u content
像这样:
<RelativeLayout
android:id="@+id/behindChessBoard"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:id="@+id/chessBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:columnCount="8"
android:rowCount="8" >
<ImageButton
android:id="@+id/cell00"
android:layout_column="0"
android:layout_row="0"
android:background="@color/chessTan"
android:scaleType="fitXY"
android:contentDescription="00" />
<ImageButton
android:id="@+id/cell01"
android:layout_column="1"
android:layout_row="0"
android:scaleType="fitXY"
android:background="@color/chessBrown"
android:contentDescription="01" />
... so on and so forth...
</GridLayout>
</RelativeLayout>
当您将高度和宽度设置为“匹配父项”时,它不会垂直居中显示内容。将高度和宽度设置为
wrap\u content
像这样:
<RelativeLayout
android:id="@+id/behindChessBoard"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridLayout
android:id="@+id/chessBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:columnCount="8"
android:rowCount="8" >
<ImageButton
android:id="@+id/cell00"
android:layout_column="0"
android:layout_row="0"
android:background="@color/chessTan"
android:scaleType="fitXY"
android:contentDescription="00" />
<ImageButton
android:id="@+id/cell01"
android:layout_column="1"
android:layout_row="0"
android:scaleType="fitXY"
android:background="@color/chessBrown"
android:contentDescription="01" />
... so on and so forth...
</GridLayout>
</RelativeLayout>
将android:gravity=“center”放在父级相对布局中。如果我的答案符合您的需要,请接受此答案。所以它有助于以后的追随者。将安卓:gravity=“center”放在父级相对布局中。如果我的答案满足您的需要,那么接受这个答案。这样对后来的追随者有帮助。