Android ListView padding for content without moving ListView height?

后端 未结 3 1463
南方客
南方客 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:23

    The trick is to include android:clipToPadding="false" in your view definition, e.g.:

    <ListView android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="16dip"
        android:paddingBottom="16dip"
        android:clipToPadding="false" />
    
    0 讨论(0)
  • 2021-01-31 16:27

    Why not just check the position of the item relative to the size of your list:

    Padding:

     public View getView(int position, View convertView, ViewGroup parent)
        {
             //recycle views and whatever else you normally would do goes here..
             //...
             //...
    
             if (position == 0){
                convertView.setPadding(0, 10, 0, 0); //padding on top for top item
             }
             else if (position == getCount() - 1){
                convertView.setPadding(0, 0, 0, 10); //padding on bottom for bottom item
             }
             else{
                convertView.setPadding(0, 0, 0, 0); //no padding
             }
    }
    

    For margins use

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(left, top, right, bottom);
    convertView.setLayoutParams(lp);
    

    Ex:

    public View getView(int position, View convertView, ViewGroup parent)
        {
                 //recycle views and whatever else you normally would do goes here..
                 //...
                 //...
                 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,        LinearLayout.LayoutParams.WRAP_CONTENT);
                 if (position == 0){
                     lp.setMargins(0, 10, 0, 0); //margin on top for top item
                 }
                 else if (position == getCount() - 1){
                    lp.setMargins(0, 10, 0, 10); //margin on bottom for bottom item
                 }
                 else{
                    lp.setMargins(0, 0, 0, 0); //no margin
                 }
                 convertView.setLayoutParams(lp);
        }
    

    this is provided you've implemented the getCount() method properly for your adapter.

    0 讨论(0)
  • 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:

    <?xml version="1.0" encoding="utf-8"?>
    <View xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="10dp"/>
    

    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.

    0 讨论(0)
提交回复
热议问题