解决底部Button遮挡ListView最后一项内容的bug

项目中ListView和Button经常是一起使用的,用ListView来展示数据,用Button来提交修改的数据或对修改的数据进行确定操作。

假如使用线性布局的话ListView会盖住整个Button,使用相对布局来操作,然后Button按钮再使用android:layout_alignParentBottom="true"

让再底部显示,这样显示是没有问题的,但是如果ListView数据过多,当ListView翻到底部的时候会发现Button会遮挡最后一个item的内容,如下图:

你会发现,“你好19”所在的item被遮挡住了。下面就讲下解决这个问题的方法,总体思路是这样的:获得底部Button的高度,再让ListView的marginBottom等于这个高度。

list = (ListView) findViewById(R.id.list);
        Button btn=(Button)findViewById(R.id.btn);
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
        btn.measure(w, h); 
        int height =btn.getMeasuredHeight(); 
        
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
        lp.setMargins(0, 0, 0, height);
        list.setLayoutParams(lp);

最后结果如下图:

posted @ 2014-11-13 10:21  用户没有名  阅读(3115)  评论(0编辑  收藏  举报