Reputation: 1867
I'm trying to center my GridView in a RelativeLayout. I set the GridView's height to wrap_content and gravity of the RelativeLayout is center, but the GridView is still aligned at the top. Here is what I have:
<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>
How can I center this? Thanks
Upvotes: 0
Views: 597
Reputation: 1242
Please add these to your GridView: android:layout_centerVertical="true"
and android:layout_centerHorizontal="true"
Upvotes: 1
Reputation: 199805
Instead of using android:gravity
on your RelativeLayout
, you should use android:layout_centerVertical:
<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>
Upvotes: 1
Reputation: 5096
Remove center gravity from the RelativeLayout. add this to your GridView:
android:layout_gravity="center_vertical"
Upvotes: 0