**获得View的宽度 测量这个view,最后通过getMeasuredWidth()获取宽度.**
2017-06-27 11:47
477 查看
public static 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 childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width); int lpHeight = p.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } view.measure(childWidthSpec, childHeightSpec); } public static int getViewWidth(View view) { measureView(view); return view.getMeasuredWidth(); }
相关文章推荐
- Android之View的知识(getWidth() 和getMeasuredWidth区别 如何在oncreate获取宽高)
- View的getWidth()和getMeasuredWidth()有什么区别吗?如何在onCreate中拿到View的宽度和高度?
- 自定义view2--getMeasuredWidth和getWidth方法区分
- UITableView获取cell的row值(解决自定义cell里的button通过点击无法获得cell的indexpath的问题
- HBuilder mui 通过getWebviewById获取入口文件
- Bitmap通过getWidth和getHeight获取尺寸不符
- 【android学习】imageview通过getDrawingCache获取图像
- android之view获取getWidth()和getHeight()
- getWidth deprecated [获取屏幕宽度]
- Android 关于view的getLayoutParams().width,getWidth(),getMeasuredWidth();
- Android中View窗口getWidth和getMeasuredWidth的差别
- 想在自己的android应用中获得当天的天气情况,这该怎么做呢?不用担心。中国国家气象局提供了获取所在城市天气预报信息接口。通过这个接口,我们就可以获取天气信息了。
- 关于view.getWidth等获取值的单位是什么?px/dp/sp
- iOS中获取用户所点击的位置信息,并通过这个位置信息得到tableView的中的cell的indexPath.section
- MDI中通过GetActiveView获得VIEW
- Android中View窗体getWidth和getMeasuredWidth的区别
- 关于通过getWidth与getHeight获取bitmap的尺寸与其实际尺寸不符合问题
- 再一个Activity的子Fragment中,获取这个子Fragment布局中的一个ImageView的width总是为0的问题
- Android 关于view的getLayoutParams().width,getWidth(),getMeasuredWidth();
- Bitmap通过getWidth和getHeight获取尺寸不符