How Can I Force A Gridview To Use The Whole Screen (regardless Of Display Size)?
I've got the following layout file, which has a GridView and an ImageView behind that as the background.
Solution 1:
This will work well. Don't forget about vertical spacing.
publicclassMyAdapterextendsBaseAdapter {
publicstaticintROW_NUMBER=5;
publicMyAdapter(Context mContext, ArrayList<String> list) {
this.context = mContext;
lstDate = list;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item, null);
}
// we need to decrease cell height to take into account verticalSpacing for GridViewintcellHeight= StrictMath.max(parent.getHeight() / ROW_NUMBER - parent.getContext().getResources().getDimensionPixelOffset(R.dimen.grid_spacing), 320);
AbsListView.LayoutParamsparam=newAbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
parent.getHeight() / ROW_NUMBER);
convertView.setLayoutParams(param);
return convertView;
}
Solution 2:
Not automatically.
In particular, your cells are text. Android is not exactly in position to guess how big the text should be to accomplish your aims, particularly once you take word-wrap into account.
The point of GridView
is to have "un-used white space at the bottom of the display", if you do not have enough data to fill the screen, so that it can flexibly handle multiple screen sizes and also accommodate scrolling if there is more data. If you are aiming for something akin to the dashboard pattern, consider using DashboardLayout
or something along those lines.
Solution 3:
Have you tried android:layout_weight="1"
?
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><GridViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/gridview"android:layout_width="match_parent"android:layout_height="match_parent"android:columnWidth="90dp"android:numColumns="auto_fit"android:verticalSpacing="10dp"android:horizontalSpacing="10dp"android:stretchMode="columnWidth"android:gravity="center"android:layout_weight="1"/></LinearLayout>
Or
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><GridViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/gridview"android:layout_width="match_parent"android:layout_height="match_parent"android:columnWidth="90dp"android:numColumns="auto_fit"android:verticalSpacing="10dp"android:horizontalSpacing="10dp"android:stretchMode="columnWidth"android:gravity="center"
/></LinearLayout>
Hope that helps?
Post a Comment for "How Can I Force A Gridview To Use The Whole Screen (regardless Of Display Size)?"