使用标志
if(build.version.sdk ou int>=build.version ou codes.kitkat){
窗口w=getWindow();
w.addflags(windowmanager.layoutParams.flag ou layout_no_limits);
w.setSoftInputMode(windowManager.layoutParams.Soft_-input_-adjust_-pan);
w.setsoftinputmode(windowmanager.layoutParams.soft输入调整大小);
}
< /代码>
然后用这两种方法来推动你的观点
@suppresslint(“newapi”)。
公共静态布尔型HasImmersive(上下文CTX){
如果(!)缓存({)
if(build.version.sdk_int<build.version_codes.kitkat){
hasemersive=false;
缓存=真;
返回错误;
}
显示d=(windowManager)ctx.getSystemService(context.window_service)).getDefaultDisplay();
displayMetrics realDisplayMetrics=新建displayMetrics();
d.获取RealMetrics(RealDisplayMetrics);
int realheight=realDisplayMetrics.heightPixels;
int realwidth=realDisplayMetrics.widthPixels;
displayMetrics displayMetrics=新建displayMetrics();
d.getmetrics(显示指标);
int displayheight=displaymetrics.heightpixels;
int displaywidth=displaymetrics.widthpixels;
hasemersive=(realwidth>displaywidth)(realheight>displayheight);
缓存=真;
}
还具有沉浸感;
}
< /代码>
相应地应用填充
if(hasemersive(this))。{
yourview.setpadding(0,0,0,getSoftButtonsBarHeight());
}
< /代码>
获取系统高度软键
@suppresslint(“newapi”)。
private int getSoftButtonsBarHeight()。{
//GetRealMetrics仅在API 17和+
if(build.version.sdk_int>=build.version_codes.jelly_bean_mr1){
displayMetrics metrics=new displayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(度量);
int usableheight=metrics.heightpixels;
getWindowManager().getDefaultDisplay().getRealMetrics(度量);
int realheight=metrics.heightpixels;
如果(realheight>usableheight)
返回realheight-usableheight;
其他的
返回0;
}
返回0;
}
< /代码>
屏幕截图
编辑:to preserve thefitssystemwindows=“true”behavior for the notification bar,add theflag ou layout_no_limits
and other flags after the window insets have been applied to yourview
which hasfitssystemwindows
set totrue
。这将确保在应用“无限制”标志之前,插页已正常应用。
yourviewWithFitssSystemWindows.setOnApplyWindowInsetsListener(new view.OnApplyWindowInsetsListener()。{
@重写
公共窗口插入到应用窗口插入(视图V,窗口插入插入){
if(insets.getSystemWindowInsetTop()==0)返回insets;//这是必需的。
v.onApplyWindowInsets(插入);
getwindow().addflags(windowmanager.layoutParams.flag ou layout_no_limits);
//添加其他标志
返回插图;
}
(});
< /代码>
这样,再加上设置底部填充,可以得到所需的结果。
然后用这两种方法来推动你的观点
@SuppressLint("NewApi")
public static boolean hasImmersive(Context ctx) {
if (!cached) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
hasImmersive = false;
cached = true;
return false;
}
Display d = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
hasImmersive = (realWidth > displayWidth) || (realHeight > displayHeight);
cached = true;
}
return hasImmersive;
}
相应地应用填充
if (hasImmersive(this)) {
yourView.setPadding(0, 0, 0, getSoftButtonsBarHeight());
}
获取系统高度软键
@SuppressLint("NewApi")
private int getSoftButtonsBarHeight() {
// getRealMetrics is only available with API 17 and +
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}
屏幕截图
编辑:保存fitsSystemWindows="true"
通知栏的行为,添加FLAG_LAYOUT_NO_LIMITS
窗口插入后的其他标志已应用于View
哪个有fitsSystemWindows
设置为true
. 这将确保在应用“无限制”标志之前,插页已被正常应用。
yourViewWithFitsSystemWindows.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if(insets.getSystemWindowInsetTop() == 0) return insets; // This is needed.
v.onApplyWindowInsets(insets);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// add other flags
return insets;
}
});
这样,再加上设置底部填充,可以得到所需的结果。