您的位置:首页 > 其它

屏幕尺寸,控件尺寸的获取方法

2015-07-18 23:53 218 查看
在Android中,有时需要获取内容区域,标题区域,状态区域,控件的尺寸,如长宽高,才能精确的布局,使界面更美观。下面就把各种用到的求尺寸的方法汇总,代码如下:
一、获取控件尺寸
1
方法一:
2
View layout = findViewById(R.id.popwindow_btn);
3
ViewTreeObserver 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
@Override
23
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
* @return
5
*/
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
* @return
5
*/
6
public 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

15
public int getTitleBarHeight(){
16
return getActionBar().getHeight();
17
}
四、获取手机屏幕尺寸
1
/**
2
* 获取手机屏幕尺寸 width,height
3
*
4
* @return
5
*/
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
}
五、获取内容区域尺寸
1
int 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: