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

前端 未结 3 1303
不思量自难忘°
不思量自难忘° 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: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.

提交回复
热议问题