(Android) How to fill ListView background and preserve header transparency

前端 未结 3 1294
不思量自难忘°
不思量自难忘° 2021-02-19 11:18

I have a custom image background that fills the entire screen behind a ListView.

The ListView has a header that contains some data, then a transparent 10dp margin(allowi

相关标签:
3条回答
  • 2021-02-19 11:33

    I have found solution to this, if anyone still needs it. I just made drawable that has top colored as header background like this:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:right="-45dp" android:left="-45dp" android:bottom="-45dp">
        <shape>
            <solid android:color="@color/gray_back" />
            <stroke android:width="40dp" android:color="@color/colorPrimaryDark" />
        </shape>
    
    </item>
    

    I know it's possible to use NavigationView now but I needed quick fix for current app.

    0 讨论(0)
  • 2021-02-19 11:46

    You need to make the items in the ListView transparent by setting following on the ListView:

    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    

    Then your items Layout needs to have whatever color/image (this will be put on top of the transparency).

    This should work but will be heavy work on Android as transparency really takes its toll on Android especially when it's in a ListView where there is a lot of UI updates.

    0 讨论(0)
  • 2021-02-19 11:46

    I hacked it by adding a colored footer. The footer was a LinearLayout containing a View called "fill" set to 5dp height w/ my desired bg color.

    After populating & scaling my list header (prior code), I then adjusted the footer to fill in the remaining screen space:

            ...
    
        int totHeight = getTotalHeightofListView();
    
        if((mHeader.getMeasuredHeight() + totHeight) < mList.getMeasuredHeight())
            addFooterFill(mHeader.getMeasuredHeight() + totHeight);
    }
    
    protected int getTotalHeightofListView() {
    
        ListAdapter LvAdapter = mList.getAdapter();
        int totHeight = 0;
        for (int i = 0; i < mAdapter.getCount(); i++) {
            View mView = mAdapter.getView(i, null, mList);
            mView.measure(
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            totHeight += mView.getMeasuredHeight();
        }
    
        return totHeight; 
    }
    
    protected void addFooterFill(int sizeUsed){
    
        View fill = (View) mFooter.findViewById(R.id.fill);
    
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) fill.getLayoutParams();
        params.height = mList.getMeasuredHeight() - sizeUsed;
        fill.setLayoutParams(params);
    }
    

    In theory this should work even if you already have a footer. You're essentially just adjusting the height of a view within the footer.

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