当我的应用程序在Android 4.4+上运行时,我正在使用沉浸模式。 (http://developer.android.com/training/system-ui/immersive.html)
我的活动确实以全屏显示,并且我通过使用setOnSystemUiVisibilityChangeListener
解决了按下音量键的问题。我也有类似的代码可将对话框置于沉浸式模式。
但是,当显示对话框时,导航。条在屏幕上跳跃,然后立即后退。取消对话框后,情况更糟-导航。条跳并调整后面活动的大小。
以下是我的课程,用于支持沉浸式模式。只需在每个Activity的onResume上调用它,并且在构建每个对话框时也会调用一个单独的函数。
我是否错过任何标志或回调,还是已知的Android问题?
public class ImmersiveModeHelper {
public ImmersiveModeHelper(Activity activity)
{
mActivity = activity;
}
@SuppressLint("NewApi")
public void supportFullScreenImmersiveMode()
{
MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveMode: ");
// Support full-screen immersive mode on Android 4.4 and up
if (Build.VERSION.SDK_INT >= 19)
{
// Get the needed flags by reflection and use them
try
{
final int immersiveFlag = View.class.getField("SYSTEM_UI_FLAG_IMMERSIVE_STICKY")
.getInt(null);
final int hideNavigationFlag = View.class
.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION").getInt(null);
final int fullScreenFlag = View.class.getField("SYSTEM_UI_FLAG_FULLSCREEN").getInt(
null);
// Set the flags to the window decor view
mActivity.getWindow().getDecorView()
.setSystemUiVisibility(immersiveFlag | hideNavigationFlag | fullScreenFlag);
// Set a callback to be called when visibility changes
// (workaround
// for volume keys)
mActivity
.getWindow()
.getDecorView()
.setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener()
{
@Override
public void onSystemUiVisibilityChange(int visibility)
{
MyLog.d("ImmersiveModeHelper.supportFullScreenImmersiveMode().new OnSystemUiVisibilityChangeListener() {...}: onSystemUiVisibilityChange: " +
"");
if ((visibility & (immersiveFlag | hideNavigationFlag)) == 0)
{
Handler uiHandler = UiThreadUtils.getUiHandler();
uiHandler.removeCallbacks(mHideSystemUiCallback);
uiHandler.postDelayed(mHideSystemUiCallback,
HIDE_SYSTEM_UI_DELAY_MILLI);
}
}
});
} catch (Exception e)
{
e.printStackTrace();
MyLog.e("ImmersiveModeHelper: supportFullScreenImmersiveMode: couldn't support immersive mode by reflection");
}
} else
{
MyLog.i("ImmersiveModeHelper: supportFullScreenImmersiveMode: not supported on this platform version");
}
}
public static void supportFullScreenImmersiveModeForDialog(final Dialog dlg)
{
MyLog.d("ImmersiveModeHelper: supportFullScreenImmersiveModeForDialog: ");
// Support full-screen immersive mode on Android 4.4 and up
if (Build.VERSION.SDK_INT >= 19)
{
final Window dlgWindow = dlg.getWindow();
// Get the needed flags by reflection and use them
try
{
final int immersiveFlag = View.class.getField("SYSTEM_UI_FLAG_IMMERSIVE_STICKY")
.getInt(null);
final int hideNavigationFlag = View.class
.getField("SYSTEM_UI_FLAG_HIDE_NAVIGATION").getInt(null);
final int fullScreenFlag = View.class.getField("SYSTEM_UI_FLAG_FULLSCREEN").getInt(
null);
// Set the flags to the window decor view
int flags = dlgWindow.getDecorView().getSystemUiVisibility();
flags |= (immersiveFlag | hideNavigationFlag | fullScreenFlag);
dlgWindow.getDecorView().setSystemUiVisibility(flags);
// Set a callback to be called when visibility changes
// (workaround for volume keys)
dlgWindow.getDecorView().setOnSystemUiVisibilityChangeListener(
new View.OnSystemUiVisibilityChangeListener()
{
@Override
public void onSystemUiVisibilityChange(int visibility)
{
MyLog.d("ImmersiveModeHelper.supportFullScreenImmersiveModeForDialog(...).new OnSystemUiVisibilityChangeListener() {...}: onSystemUiVisibilityChange: ");
if ((visibility & (immersiveFlag | hideNavigationFlag)) == 0)
{
Runnable hideSystemUiCallback = new Runnable()
{
@Override
public void run()
{
supportFullScreenImmersiveModeForDialog(dlg);
}
};
Handler uiHandler = UiThreadUtils.getUiHandler();
uiHandler.removeCallbacks(hideSystemUiCallback);
uiHandler.postDelayed(hideSystemUiCallback,
HIDE_SYSTEM_UI_DELAY_MILLI);
}
}
});
} catch (Exception e)
{
e.printStackTrace();
MyLog.e("ImmersiveModeHelper: supportFullScreenImmersiveMode: couldn't support immersive mode by reflection");
}
} else
{
MyLog.i("ImmersiveModeHelper: supportFullScreenImmersiveMode: not supported on this platform version");
}
}
private Activity mActivity;
private Runnable mHideSystemUiCallback = new Runnable()
{
@Override
public void run()
{
supportFullScreenImmersiveMode();
}
};
private static final int HIDE_SYSTEM_UI_DELAY_MILLI = 0;
}
从Google API:最好包含其他系统UI标志(例如SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION和SYSTEM_UI_FLAG_LAYOUT_STABLE),以防止在隐藏和显示系统栏时调整内容的大小。
您还应确保同时隐藏操作栏和其他UI控件。此代码段演示了如何隐藏和显示状态和导航栏,而无需调整内容的大小:
// This snippet hides the system bars.
private void hideSystemUI() {
// Set the IMMERSIVE flag.
// Set the content to appear under the system bars so that the content
// doesn't resize when the system bars hide and show.
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
mDecorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
希望这会有所帮助。
在Dialog或BottomSheetDialogFragment中,您必须实现对我有用的解决方案。
步骤1:
在您的对话框或BottomSheetDialog中,使用onActivityCreated方法编写此代码,
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
var viewParent = view
while (viewParent is View) {
viewParent.fitsSystemWindows = false
viewParent.setOnApplyWindowInsetsListener { _, insets -> insets }
viewParent = viewParent.parent as View?
}
}
步骤2:另外,重写以下方法:
override fun setupDialog(dialog: Dialog, style: Int) {
super.setupDialog(dialog, style)
dialog?.window?.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
}
现在看魔术:)