Android ListView padding for content without moving ListView height?

后端 未结 3 1454
南方客
南方客 2021-01-31 15:46

I have a ListView with a bunch of items inside it. How do I make the top and bottom items have a top margin of 10dp for top item and bottom margin of 10dp for bottom item? Now I

3条回答
  •  有刺的猬
    2021-01-31 16:28

    You can get space at the top and bottom of your ListView, inside the scrolling region, by using headers and footers. Here's a quick example:

    Define your filler view, which I named header_footer.xml:

    
    
    

    Now your onCreate method looks like this, assuming you named your ListView listView:

        final LayoutInflater inflater = 
            (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View headerFooter = inflater.inflate(R.layout.header_footer, null);
        listView.addFooterView(headerFooter);
        listView.addHeaderView(headerFooter);
    

    And then set your adapter.

提交回复
热议问题