模式对话框android,android – 如何保持沉浸式模式在对话框?

当我的活动显示自定义对话框时,如何保持新的沉浸式模式?

实际上,我使用这个代码来维持对话框中的沉浸式模式,但是在这种方式下,当我开始自定义对话框时,NavBar的显示时间不到一秒钟,然后消失。

这里有一个视频解释更好的问题(当NavBar出现时,看看屏幕底部):http://youtu.be/epnd5ghey8g

如何避免这种行为?

在我的申请中的所有活动的父亲:

public abstract class ImmersiveActivity extends Activity {

@SuppressLint("NewApi")

private void disableImmersiveMode() {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

getWindow().getDecorView().setSystemUiVisibility(

View.SYSTEM_UI_FLAG_FULLSCREEN);

}

}

@SuppressLint("NewApi")

private void enableImmersiveMode() {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

getWindow().getDecorView().setSystemUiVisibility(

View.SYSTEM_UI_FLAG_LAYOUT_STABLE |

View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |

View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |

View.SYSTEM_UI_FLAG_FULLSCREEN |

View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |

View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

}

}

/**

* Set the Immersive mode or not according to its state: enabled or not.

*/

protected void updateSystemUiVisibility() {

// Retrieve if the Immersive mode is enabled or not.

boolean enabled = getSharedPreferences(Util.PREF_NAME, 0).getBoolean(

"immersive_mode_enabled", true);

if (enabled) enableImmersiveMode();

else disableImmersiveMode();

}

@Override

public void onResume() {

super.onResume();

updateSystemUiVisibility();

}

@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

updateSystemUiVisibility();

}

}

所有我的自定义对话框在它们的onCreate(…)方法中调用这个方法:

/**

* Copy the visibility of the Activity that has started the dialog {@link mActivity}. If the

* activity is in Immersive mode the dialog will be in Immersive mode too and vice versa.

*/

@SuppressLint("NewApi")

private void copySystemUiVisibility() {

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

getWindow().getDecorView().setSystemUiVisibility(

mActivity.getWindow().getDecorView().getSystemUiVisibility());

}

}

编辑 – 解决方案(感谢Beaver6813,看看他的答案更多的细节):

所有我的自定义对话框都以这种方式覆盖了show方法:

/**

* An hack used to show the dialogs in Immersive Mode (that is with the NavBar hidden). To

* obtain this, the method makes the dialog not focusable before showing it, change the UI

* visibility of the window like the owner activity of the dialog and then (after showing it)

* makes the dialog focusable again.

*/

@Override

public void show() {

// Set the dialog to not focusable.

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

copySystemUiVisibility();

// Show the dialog with NavBar hidden.

super.show();

// Set the dialog to focusable again.

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值