Android中的基础----如何获得LinearLayout布局的高和宽

来源:互联网 发布:linux 关闭ntpdate 编辑:程序博客网 时间:2024/06/08 18:30
存在两种情况:

1)由于LinearLayout是View的子类,因此可以使用View.getMeasuredWidth和View.getMeasuredHeight方法来获取组件的宽度和高度。

View view =getLayoutInflater().inflate(R.layout.main,null);LinearLayout linearlayout =(LinearLayout)view.findViewById(R.id.linearlayout);//measure的参数为0即可linearlayout.measure(0,0);//获取组件的宽度int width=linearlayout.getMeasuredWidth();//获取组件的高度int height=linearlayout.getMeasuredHeight();
(如果组件的宽度或高度设置为fill_parent或match_parent。使用View.getMeasuredWidth和View.getMeasuredHeight方法获取的组件宽度和高度,当组件包含其他子组件时,所获得实际值是这些组件所占的最小宽度和最小高度。

2)如果想直接获取布局文件定义的组件的高度和宽度,可以直接使用View.getLayoutParams().width和View.getLayoutParams().height。当宽度和高度fill_parent或match_parent或者wrap_content,会返回值是MATCH_PARENT、FILL_PARENT、WRAP_CONTENT。

1 1
原创粉丝点击