我正在尝试将 GridView 置于相对布局的中心。我将GridView的高度设置为wrap_content,RelativeLayout的重力为中心,但GridView仍然在顶部对齐。这是我所拥有的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context="com.example.sixerstrivia.MainActivity" >
<GridView
android:id="@+id/dashboardGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:columnWidth="100dp"
android:horizontalSpacing="20dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" >
</GridView>
</RelativeLayout>
如何将其居中?谢谢
最佳答案
您应该使用 android:layout_centerVertical,而不是在 RelativeLayout
上使用 android:gravity
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sixerstrivia.MainActivity" >
<GridView
android:id="@+id/dashboardGrid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_centerVertical="true"
android:columnWidth="100dp"
android:horizontalSpacing="20dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" >
</GridView>
</RelativeLayout>
关于java - 在RelativeLayout中居中GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29022110/