私が見つけた最良の解決策は、コードでこれを行うことです。
これは私の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" />
そして、これは私のフラグメントに魔法をかけるコードです:
@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);
}
}); }
}