自定义布局获取宽高问题
2016-01-05 11:40
232 查看
private void initView(){
//初始化布局
//假如想获取子布局高度
View header = LayoutInflater.from(this).inflate(R.layout.main,null);
measureView(header);
int headerHeight = header.getMeasuredHeight();
//其他操作
}
/**
* 获取宽高前,要通知父布局自己需要占用的大小
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int wid
8f41
th = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECFIED);
}
view.measure(width, height);
}
//初始化布局
//假如想获取子布局高度
View header = LayoutInflater.from(this).inflate(R.layout.main,null);
measureView(header);
int headerHeight = header.getMeasuredHeight();
//其他操作
}
/**
* 获取宽高前,要通知父布局自己需要占用的大小
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int wid
8f41
th = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECFIED);
}
view.measure(width, height);
}
相关文章推荐
- html5+d3 svg 线条、图形颜色渐变动画
- 【SQL 编程你也行】SQL Server 2012新功能之函数:choose函数
- Web.xml配置详细
- linux 安装memcached libmemcached libevent freetds
- SLAM:使用G2O-ORB-SLAM
- tomcat配置优化
- IDA 远程调试 Android so
- 模型验证规则
- 代码重构-4 通用方法 用 static
- JAVA网络编程之获取网络资源
- eclipse打开文件所在文件夹的方法
- java加载第三方jar并实例化类
- 猴子的android规范
- BlockingCollection的使用
- Chrome开发者工具不完全指南:(三、性能篇)
- Chrome开发者工具不完全指南:(三、性能篇)
- 编程中位运算应用
- printf 打印颜色
- Android LayoutInflater详解
- 关系型数据库基础