全屏Activity弹出对话框如何防止全屏模式失效

Activity的全屏模式如下,因为兼容android6.0,就直接动态代码实现:

 
  1. //在activity的onCreate方法中先调用此方法在setContent进行实现全屏模式

  2. private void setFullScreenMode(){

  3. //设置永不休眠模式

  4. getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,

  5. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

  6. //隐藏系统工具栏方式一

  7. getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

  8. //隐藏底部导航栏

  9. View decorView = getWindow().getDecorView();

  10. if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {

  11. decorView.setSystemUiVisibility(View.GONE);

  12. } else if (Build.VERSION.SDK_INT >= 19) {

  13. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  14. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

  15. | View.SYSTEM_UI_FLAG_FULLSCREEN);

  16. }

  17. decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){

  18. @Override

  19. public void onSystemUiVisibilityChange(int visibility) {

  20. View decorView = getWindow().getDecorView();

  21. int uiState=decorView.getSystemUiVisibility();

  22. if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {

  23. if(uiState!=View.GONE) decorView.setSystemUiVisibility(View.GONE);

  24. } else if (Build.VERSION.SDK_INT >= 19) {

  25. if(uiState!=(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  26. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

  27. | View.SYSTEM_UI_FLAG_FULLSCREEN))

  28. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  29. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

  30. | View.SYSTEM_UI_FLAG_FULLSCREEN);

  31. }

  32. }

  33. });

  34. }

然而,现实对话框的时候,在华为的平板上,下面本来被隐藏的导航栏显示了出来,而且activity被上下压缩,很不美观,最开始以为是theme引起的,结果然并卵,网上也没有查到相关资料,好吧,我没就从dialog入手吧。

dialog内部其实也是一个Window,然后将视图资源加载进去,那么初步怀疑出现这种情况的原因:dialog弹出来后用了dialog的window,这个window很显然是默认的属性不是全屏的。

既然上面实现全屏模式是通过设置Window的属性,那么就依葫芦画瓢将dialog的window也想如上设置,将上面方法更改,参数传递一个Window进去,代码如下:

 
  1. public void showDialog(){

  2. AlertDialog dialog=new AlertDialog.Builder(_rootActivity)

  3. .setTitle("确定删除班级"+nameView.getText().toString()+"吗?")

  4. .setPositiveButton("是", this)

  5. .setNegativeButton("否", null)

  6. .create();

  7. final Window window=dialog.getWindow();

  8. setFullScreenMode(window);

  9. dialog.show();

  10. }

  11. private void setFullScreenMode(Window window){

  12. //设置永不休眠模式

  13. window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,

  14. WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

  15. //隐藏系统工具栏方式一

  16. window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

  17. //隐藏底部导航栏

  18. View decorView = window.getDecorView();

  19. if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {

  20. decorView.setSystemUiVisibility(View.GONE);

  21. } else if (Build.VERSION.SDK_INT >= 19) {

  22. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  23. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

  24. | View.SYSTEM_UI_FLAG_FULLSCREEN);

  25. }

  26. decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){

  27. @Override

  28. public void onSystemUiVisibilityChange(int visibility) {

  29. View decorView = getWindow().getDecorView();

  30. int uiState=decorView.getSystemUiVisibility();

  31. if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {

  32. if(uiState!=View.GONE) decorView.setSystemUiVisibility(View.GONE);

  33. } else if (Build.VERSION.SDK_INT >= 19) {

  34. if(uiState!=(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  35. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

  36. | View.SYSTEM_UI_FLAG_FULLSCREEN))

  37. decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

  38. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

  39. | View.SYSTEM_UI_FLAG_FULLSCREEN);

  40. }

  41. }

  42. });

  43. }

Ok,经过测试上面问题完全解决,弹出来的对话框不会在改变activity的宽高了

 

 

    原文作者:小菜鸟hahha
    原文地址: https://blog.csdn.net/qq_29443203/article/details/109221592
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞