问题
Is it possible to add extra space (kinda like an empty row) to the bottom of a GridView?
I would like it so when you scroll down to the bottom of a GridView, there will be an extra 50dp of empty space. I tried setting paddingBottom
to 50dp, but it didn't seem to change anything.
回答1:
If I understand you right, it should look like this:

For that an easy solution is mentioned here by Maciej: How to add extra space inside at the bottom of a GridView
You just need to add the following in your GridView Layout:
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="50dp"
android:clipToPadding="false"/>
The most important part is the clipToPadding attribute which has to be set to false.
You can also see the according blog post from Google, where this solution is mentioned: https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M
回答2:
You can realize own GridViewAdapter extends ArrayAdapter and:
@Override
public int getCount() {
return gridArray.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//...
if (getCount()-1 == position) {
convertView.setPadding(0, 0, 0, 50);
} else {
convertView.setPadding(0, 0, 0, 0);;
}
return convertView;
}
where 50 is bottom padding in pixels see View.setPadding(int, int, int, int)
and you GridItemView layout mast be android:layout_height="wrap_content"
回答3:
Try adding paddingBottom to the container its inside of instead. PaddingBottom on the listview won't work because the listview would add it after its last element, which won't be on screen. It kind of pretends that it has infinite height.
Another solution would be to use a RelativeLayout and shove a view with a fixed height but no graphical elements inbetween the bottom and the GridView, with the GridView set to layout_above that view.
回答4:
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<GridView
android:id="@+id/gridUsers"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="true"
android:columnWidth="100dp"
android:fastScrollEnabled="true"
android:gravity="center"
android:numColumns="auto_fit"
android:scrollbars="none"
android:stretchMode="columnWidth" >
</GridView>
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp"
android:textColor="#f1f1f1"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linlaProgressBar"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
I use this in an app where the LinearLayout
with the id of linlaProgressBar
serves as a container for a ProgressBar
when loading more data.
This example shows a GridView
but replacing that with a ListView
will achieve the same result.
回答5:
With a ListView you would add a footer, unfortunately there's no support for that in GridView. I would suggest that adding an empty row with the height that you want is the cleanest solution.
来源:https://stackoverflow.com/questions/15962705/add-extra-space-to-bottom-of-a-gridview-or-listview