Android适配全面屏
测试了很多方法,下面这个是我找到的可以用的
首先sdk的版本要在28及以上
然后使用代码
在setContentView之前插入代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//实现全屏
getWindow().setDecorFitsSystemWindows(false);
//隐藏底部的navigation操作栏
getWindow().getInsetsController().hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
//将底部的navigation操作栏弄成透明,滑动显示,并且浮在上面
getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 如果当前版本小于HONEYCOMB版本,即3.0版本
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//让内容延伸进刘海
WindowManager.LayoutParams params = window.getAttributes();
//这里api是9.0以后的。所以这里需要添加版本判断
params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
window.setAttributes(params);
//设置成沉浸式
int flags = /*View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | */View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
int visibility = window.getDecorView().getSystemUiVisibility();
visibility |= flags;
window.getDecorView().setSystemUiVisibility(visibility);
}
else {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
Inf.sc_w = outMetrics.widthPixels;//得到屏幕尺寸
Inf.sc_h = outMetrics.heightPixels;
setContentView(R.layout.welcome);
}