Activity的全屏模式如下,因为兼容android6.0,就直接动态代码实现:
//在activity的onCreate方法中先调用此方法在setContent进行实现全屏模式
private void setFullScreenMode(){
//设置永不休眠模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//隐藏系统工具栏方式一
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
//隐藏底部导航栏
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
decorView.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){
@Override
public void onSystemUiVisibilityChange(int visibility) {
View decorView = getWindow().getDecorView();
int uiState=decorView.getSystemUiVisibility();
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
if(uiState!=View.GONE) decorView.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
if(uiState!=(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN))
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
}
});
}
然而,现实对话框的时候,在华为的平板上,下面本来被隐藏的导航栏显示了出来,而且activity被上下压缩,很不美观,最开始以为是theme引起的,结果然并卵,网上也没有查到相关资料,好吧,我没就从dialog入手吧。
dialog内部其实也是一个Window,然后将视图资源加载进去,那么初步怀疑出现这种情况的原因:dialog弹出来后用了dialog的window,这个window很显然是默认的属性不是全屏的。
既然上面实现全屏模式是通过设置Window的属性,那么就依葫芦画瓢将dialog的window也想如上设置,将上面方法更改,参数传递一个Window进去,代码如下:
public void showDialog(){
AlertDialog dialog=new AlertDialog.Builder(_rootActivity)
.setTitle("确定删除班级"+nameView.getText().toString()+"吗?")
.setPositiveButton("是", this)
.setNegativeButton("否", null)
.create();
final Window window=dialog.getWindow();
setFullScreenMode(window);
dialog.show();
}
private void setFullScreenMode(Window window){
//设置永不休眠模式
window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//隐藏系统工具栏方式一
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
//隐藏底部导航栏
View decorView = window.getDecorView();
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
decorView.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){
@Override
public void onSystemUiVisibilityChange(int visibility) {
View decorView = getWindow().getDecorView();
int uiState=decorView.getSystemUiVisibility();
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
if(uiState!=View.GONE) decorView.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
if(uiState!=(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN))
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
}
});
}
Ok,经过测试上面问题完全解决,弹出来的对话框不会在改变activity的宽高了