Android中的基础----如何获得LinearLayout布局的高和宽
16lz
2021-01-24
存在两种情况:
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。
更多相关文章
- Android列表组件ListView使用详解之数据为空提示信息
- Android的ScrollView和HorizontalScrollView-android学习之旅(四
- Android中button自适应textsize
- android TextView 如何动态获取宽度
- Eclipse 安装配置指南
- TimePicker的一些使用方法
- android软键盘遮挡布局
- Application Fundamentals--Application Components(应用组件)
- android中获取字符串的宽度