How do I center a GridView in its LinearLayout parent?
GridView is not behaving like it is supposed to. This screenshot shows that the GridView (in landscape mode) is flushed left. I want it centered.
开发者_开发百科This is the XML layout for the GridView.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/templatelandscape"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<GridView
android:id="@+id/commandsbarlandscape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:padding="0dp"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:columnWidth="52dp"
android:stretchMode="spacingWidth"
android:gravity="fill_horizontal" />
</LinearLayout>
What am I doing wrong ?
I don't know if you resolved your problem (i hope yes, and also i'm very late on answering on this post!), but here is a clue:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<GridView
android:id="@+id/gridview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:stretchMode="spacingWidth" >
</GridView>
</LinearLayout>
Unfortunately this work only if there is 1 content on the grid. Maybe someone can improve it!
android:layout_height="wrap_content"
has no meaning for widgets that have their own scrolling, likeGridView
.android:layout_alignParentTop="true"
andandroid:layout_centerInParent="true"
are forRelativeLayout
, notLinearLayout
.
Get rid of your android:layout_weight="1.0"
, change your android:layout_height
to "fill_parent"
or a specific height, change the LinearLayout
to a RelativeLayout
, and you may be in better shape.
set the value of LinearLayout with following attribute
android:gravity="center"
.
and remove the following line from gridview
android:layout_centerInParent="true"
Try switching to RelativeLayout and remove the horizontal padding from the grid view. Horizontal padding was causing my grid to shift off of center. I added a view container around the grid item layout to create the padding.
Your GridLayout should be like this..
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/templatelandscape"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<GridView
android:id="@+id/commandsbarlandscape"
android:background="@android:color/darker_gray"
android:layout_gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:columnWidth="52dp"
android:stretchMode="none"
android:gravity="fill_horizontal" />
I just place android:layout_gravity="center_horizontal" in parent layout so it set gridlayout in it's parent center but not it's content in center as you want. Also added android:layout_width="0dp" because it's equals to ""warp_content" property if you give "0dp" it will show you better result. I also removed that relative layout attributes you added you can observe, if your not using RelativeLayout then it's meaningless to use that properties that do nothing.
For testing purpose you can check that it's working correctly or not just test using following xml code
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/templatelandscape"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<GridView
android:id="@+id/commandsbarlandscape"
android:background="@android:color/darker_gray"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:numColumns="auto_fit"
android:columnWidth="52dp"
android:stretchMode="none"
android:gravity="fill_horizontal" />
</LinearLayout>
The above xml shows you that your grid layout comes in center in parent but not it's content.
Hope this explanation works for you...
Instead of setting the GridView in the LinearLayout, set it in the RelativeLayout and set the layout_centerHorizontal=true and layout_centerVertical=true in the GridView.
The best solution I found is doing this by code.
This is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dslv="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f1f1f1"
android:orientation="vertical">
<GridView
android:id="@+id/radio_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:divider="@null"
android:drawSelectorOnTop="true"
android:paddingBottom="@dimen/gridview_column_space"
android:paddingTop="@dimen/gridview_column_space"
android:columnWidth="@dimen/gridview_column_width"
android:numColumns="auto_fit"
android:verticalSpacing="@dimen/gridview_column_space"
android:horizontalSpacing="@dimen/gridview_column_space"
android:stretchMode="none"
android:scrollbars="none"
/>
<include layout="@layout/placeholder" />
And this is the code that do the magic on my fragment:
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Point point = Common.getScreenSize(getActivity());
int rowSpace = getResources().getDimensionPixelSize(R.dimen.gridview_column_space);
int rowWidth = getResources().getDimensionPixelSize(R.dimen.gridview_column_width) + rowSpace;
int minWith = (point.x / rowWidth) * rowWidth;
int padding = (point.x - minWith) / 2;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(minWith + padding * 2, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
mGridView.setPadding(padding + rowSpace / 2, 0, padding, 0);
mGridView.setLayoutParams(params);
mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}); }
}
精彩评论