代码测试的机型
状态栏高度
actionbar高度
屏幕高度
导航栏(虚拟按键栏)高度
layout宽高
总结
这里我们用的是小米8青春版手机测试(刘海屏)
小米8青春版屏幕px为1080*2280,相当于360*760dp
//获取状态栏
private int getStatusBarHeight() {
Class> c = null;
Object obj = null;
Field field = null;
int x = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
return getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
Log.d(TAG, "get status bar height fail");
e1.printStackTrace();
return 75;
}
}
Log.i(TAG, "onCreate: "+getStatusBarHeight());
上面的是获取状态栏方法,获得状态栏的高度(px)
因为这里用的小米手机是刘海屏,状态栏为20dp,通常手机屏幕的状态栏高度为25dp
dp=60/3=20dp
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
Log.i(TAG, "onPause: "+statusBarHeight);
上面的是第二种获取状态栏高度的方法(px)
P.s.该方法写在onCreate()中不可以,会获取到0
dp=60/3=20dp
//第一种获取方法
int actionBarHeight = getSupportActionBar().getHeight();
//第二种获取方法
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
上面是两种获取Actionbar高度的方法(px)
dp=168/3=56dp
P.s.该方法写在onCreate()中不可以,会获取到0
int screenWidth,screenHeight;
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Log.i(TAG, "onCreate: "+screenWidth+","+screenHeight);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth2 = dm.widthPixels;
int screenHeight2 = dm.heightPixels;
Log.i(TAG, "onCreate: "+screenWidth2+","+screenHeight2);
这是两种获取屏幕宽高的方法,单位为px
(获取到的屏幕为状态栏+actionbar+页面内容)
(不包含虚拟按键栏)
因为小米8青春版底部为虚拟按键栏,所以虚拟按键栏高度为2280-2150px=130px=43.3dp
//获取虚拟按键栏高度
public static int getNavigationBarHeight(Context context, boolean b) {
int result = 0;
//是否纯在虚拟按键栏(导航栏)
if (b) {
Resources res = context.getResources();
int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
result = res.getDimensionPixelSize(resourceId);
}
}
return result;
}
上面是获取虚拟按键栏(导航栏)高度的方法(px)
dp=130px/3=43.3dp
LinearLayout linearLayout;
linearLayout=findViewById(R.id.ll_layout);
linearLayout.measure(0,0);
Log.i(TAG,"onStop:"+linearLayout.getMeasuredHeight()+","+linearLayout.getMeasuredWidth());
Log.i(TAG,"onStop:"+linearLayout.getLayoutParams().height+","+linearLayout.getLayoutParams().width);
Log.i(TAG,"onStop:"+linearLayout.getHeight()+","+linearLayout.getWidth());
上面分为3种,第一种是获取组件测绘宽高,第二种是控件设置的宽高,第三种是控件实际显示的宽高
P.s.第一种获取的是子控件的测绘宽高
第二种设置match_parent时显示-1,设置wrap_content时显示-2
第三种方法在View没加载完成时,获取的值为0,所以写在onCreate中会获取到0(写在onStart和onResume第一次调用也为0,只是查看的话可以写在onStop中,这个时候View肯定加载完成)(可以设个监听器持续监听,直到View加载完成,宽高不为0为止)
高dp=1922px/3=640.7dp
宽dp=1080px/3=360dp
状态栏(StatusBar) 60px=20dp
ActionBar(标题栏) 168px=56dp
页面内容 1922px=640.7dp
虚拟按钮栏(导航栏) 130px=43.3dp
共计2280px=760dp
___________________________________分割线______________________________________
状态栏(StatusBar) 24dp
ActionBar(标题栏) 56dp
页面内容 560dp
高度共计640dp
华为P10屏幕分辨率1440*2560px,测出来一直是1080*1920px,后来发现是华为手机的智能分辨率功能,可以调节为屏幕密度2,3,4三种对应的分辨率