屏幕尺寸,控件尺寸的获取方法
来源:互联网 发布:linux怎么上翻 编辑:程序博客网 时间:2024/04/30 07:17
在Android中,有时需要获取内容区域,标题区域,状态区域,控件的尺寸,如长宽高,才能精确的布局,使界面更美观。下面就把各种用到的求尺寸的方法汇总,代码如下:一、获取控件尺寸1方法一:2View layout = findViewById(R.id.popwindow_btn);3ViewTreeObserver vto = layout.getViewTreeObserver();4 5 vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {6 public boolean onPreDraw() {7 if (hasMeasured == false) {8 9 int btWidth = layout.getMeasuredWidth();10 int btHeight = layout.getMeasuredHeight();11 hasMeasured = true;12 13 }14 return true;15 }16 });17 18方法二:19 ViewTreeObserver vto = mPopupBtn.getViewTreeObserver();20 vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {21 22 @Override23 public void onGlobalLayout() {24 if (hasMeasured == false) {25 int btWidth = mPopupBtn.getMeasuredWidth();26 int btHeight = mPopupBtn.getMeasuredHeight();27 hasMeasured = true;28 }29 }); 二、获取状态栏尺寸1/**2 * 状态栏高度3 *4 * @return5 */6 public int getStatusBarHeight() {7 Rect frame = new Rect();8 getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);9 int statusBarHeight = frame.top;10 11 return statusBarHeight;12 }三、获取标题栏尺寸1/**2 * 获取标题栏高度3 *4 * @return5 */6public int getTitleBarHeight() {7 int statusBarHeight = getStatusBarHeight();8 int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT)9 .getTop();10 // statusBarHeight是上面所求的状态栏的高度11 int titleBarHeight = contentTop - statusBarHeight;12 return titleBarHeight;13}14 15public int getTitleBarHeight(){16 return getActionBar().getHeight();17}四、获取手机屏幕尺寸1/**2 * 获取手机屏幕尺寸 width,height3 *4 * @return5 */6 public int[] getScreenSize() {7 DisplayMetrics dm = new DisplayMetrics();8 // 获取屏幕信息9 getWindowManager().getDefaultDisplay().getMetrics(dm);10 int screenWidth = dm.widthPixels;11 int screenHeigh = dm.heightPixels;12 13 return new int[] { screenWidth, screenHeigh };14 }五、获取内容区域尺寸1int contentViewHeight = findViewById(android.R.id.content).getHeight(); // 屏幕内容区域高度<span style="color: rgb(49, 49, 49); font-family: 微软雅黑, 黑体, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(248, 248, 248);">Android屏幕结构图</span><br style="margin-top: 0px; color: rgb(49, 49, 49); font-family: 微软雅黑, 黑体, sans-serif; font-size: 13px; line-height: 22px; background-color: rgb(248, 248, 248);" /><a target=_blank href="http://it.warmtel.com/wp-content/uploads/2015/07/QQ%E6%88%AA%E5%9B%BE20150701110554.png" rel="fancybox-post-813" title="QQ截图20150701110554" style="border: 0px; outline: 0px; background-color: rgb(248, 248, 248); font-size: 13px; margin: 0px; padding: 0px; vertical-align: baseline; text-decoration: none; color: rgb(41, 163, 235); font-family: 微软雅黑, 黑体, sans-serif; line-height: 22px;"><img class="alignnone wp-image-815" src="http://it.warmtel.com/wp-content/uploads/2015/07/QQ%E6%88%AA%E5%9B%BE20150701110554.png" alt="QQ截图20150701110554" width="536" height="414" style="border: 0px; outline: 0px; background-color: transparent; margin: 0px; padding: 0px; vertical-align: baseline;" /></a>
0 0
- 屏幕尺寸,控件尺寸的获取方法
- iOS获取屏幕尺寸的方法
- Android获取屏幕尺寸的方法
- Android 获取屏幕宽度尺寸的方法
- android 获取屏幕尺寸方法
- jquery获取屏幕尺寸方法
- android获取控件测量尺寸的方法
- 获取屏幕的尺寸信息
- 获取屏幕的大小尺寸
- 获取设备的屏幕尺寸与密度的方法
- android上准确获取屏幕尺寸的方法
- Context中获取屏幕尺寸的2种方法
- Android 获取屏幕实际显示尺寸的方法
- Context中获取屏幕尺寸的2种方法
- Android中获取屏幕物理尺寸的方法
- Android获取屏幕尺寸的4种方法
- 安卓获取当前屏幕尺寸的方法
- Android开发 获取屏幕宽高及尺寸的方法
- HDU 5281 Senior's Gun
- 数据结构-线性表
- 字符串替换
- HDU 5282 Senior's String
- Android开发超常见的内存泄露以及解决
- 屏幕尺寸,控件尺寸的获取方法
- 说说Android中的ANR
- Linux中的echo命令
- HDU 5284 wyh2000 and a string problem
- linux的管道及重定向
- [BZOJ2243][SDOI2011]染色
- Java IO及用户登录
- HDU 5285 wyh2000 and pupil
- 【总括】区间DP