Android View measure(0,0)的作用

来源:互联网 发布:姜卫鹏中超数据分析 编辑:程序博客网 时间:2024/05/21 17:00

 将view.measure(0,0)设置为0的时候表示,此时不需要考虑父控件的布局问题,直接使用getMeasuredWidth和getMeasuredHeight获取此view的自身的实际大小,可以看下面的例子:这个是在网上引用的一段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    public class Utility {
            public static void setListViewHeightBasedOnChildren(ListView listView) {
                    ListAdapter listAdapter = listView.getAdapter();
                    if (listAdapter == null) {
                            // pre-condition
                            return;
                   }
                    int totalHeight = 0;
                    for (int i = 0; i < listAdapter.getCount(); i++) {
                            View listItem = listAdapter.getView(i, null, listView);
                            listItem.measure(00);
                            totalHeight += listItem.getMeasuredHeight();
                    }
 
                    ViewGroup.LayoutParams params = listView.getLayoutParams();
                    params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                    listView.setLayoutParams(params);
            }
    }
1 0
原创粉丝点击