How to add extra space inside at the bottom of a GridView

只谈情不闲聊 提交于 2019-11-29 06:06:15

To achieve that you want you need to add a bottom padding to your GridView but also disable clipToPadding behavior. Try the XML code below:

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="50dp"
    android:clipToPadding="false"/>

You can also do it from code if you want. The benefit is that you can compute the offset dynamically in code:

gridView.setPadding(int left, int top, int right, int bottom);
gridView.setClipToPadding(false);

Note: Without disabling the clipToPadding behavior you will end up with persistent empty area on the bottom of your GridView, so disabling it is very important.


Bonus: Here is also a nice link about using clipToPadding parameter in ListView or GridView: https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M

Make custom gridview and in getview() method, use view which make space like you want.

This is what I had so far:

In your Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);

        final TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setVisibility(View.GONE);

        final GridView grid = (GridView) findViewById(R.id.gridViewCustom);


        grid.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if((firstVisibleItem + visibleItemCount) == totalItemCount){//it means you are at the end of the gridview
                    txt.setVisibility(View.VISIBLE);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

                    RelativeLayout.LayoutParams paramsGrid = (RelativeLayout.LayoutParams) grid.getLayoutParams();
                    paramsGrid.addRule(RelativeLayout.ABOVE, txt.getId());

                    txt.setLayoutParams(params);
                    grid.setLayoutParams(paramsGrid);

                }
            }
        });
    }

.xml

<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=".MainActivity" >

   <GridView
       android:id="@+id/gridViewCustom"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/textView1"
       android:layout_alignParentRight="true"
       android:layout_margin="4dp"
       android:columnWidth="80dp"
       android:gravity="center"
       android:numColumns="auto_fit"
       android:stretchMode="columnWidth" >
   </GridView>

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:text=""
       android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

To get the following result GridView with a space above the button

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_submit"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gv_someGrid"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"
        android:text="submit" />

    <GridView
        android:id="@+id/gv_someGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/btn_submit" />
</RelativeLayout>

The trick is to declare Gridview at bottom of that button then add constraint layout_above="@+id/your_id"

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:marginBottom="50dp"
    android:clipToPadding="false"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!