我有一个基于单个活动和多个片段的应用,进入片段时某些片段需要显示为全屏,退出时需要从全屏退出。我目前正在使用标志在Android Kitkat
中显示全屏,但我认为它不是最佳方式。我也读了ImmersiveMode
,但在较低的Android版本中不起作用。目前,我正在使用这些方法进行全屏输入和退出。
//This method not showing transparent status bar also navigation bar and not showing system icons either
public static void setFullscreen(Activity activity) {
if (Build.VERSION.SDK_INT > 10) {
int flags = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN;
boolean isImmersiveAvailable = android.os.Build.VERSION.SDK_INT >= 19;
if (isImmersiveAvailable) {
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
} else {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
public static void exitFullscreen(Activity activity) {
if (Build.VERSION.SDK_INT > 10) {
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
} else {
activity.getWindow()
.setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
public static void ShowHideFullscreen(boolean isFullscreen,Context cntx){
int newUiOptions = 0;
if (isFullscreen){
// BEGIN_INCLUDE (get_current_ui_flags)
// The UI options currently enabled are represented by a bitfield.
// getSystemUiVisibility() gives us that bitfield.
int uiOptions = ((Activity)cntx).getWindow().getDecorView().getSystemUiVisibility();
newUiOptions = uiOptions;
// END_INCLUDE (get_current_ui_flags)
// BEGIN_INCLUDE (toggle_ui_flags)
boolean isImmersiveModeEnabled =
((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
if (isImmersiveModeEnabled) {
Log.d(TAG, "Turning immersive mode mode off. ");
} else {
Log.d(TAG, "Turning immersive mode mode on.");
}
// Navigation bar hiding: Backwards compatible to ICS.
if (Build.VERSION.SDK_INT >= 14) {
newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Status bar hiding: Backwards compatible to Jellybean
if (Build.VERSION.SDK_INT >= 16) {
newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
}
// Immersive mode: Backward compatible to KitKat.
// Note that this flag doesn't do anything by itself, it only augments the behavior
// of HIDE_NAVIGATION and FLAG_FULLSCREEN. For the purposes of this sample
// all three flags are being toggled together.
// Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
// Sticky immersive mode differs in that it makes the navigation and status bars
// semi-transparent, and the UI flag does not get cleared when the user interacts with
// the screen.
if (Build.VERSION.SDK_INT >= 18) {
newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
}
((Activity)cntx).getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
}else {
((Activity)cntx).getWindow().clearFlags(newUiOptions);
}
}
这些是我们用来隐藏全屏动作栏的片段全屏进入或退出的方法。我们使用它们在onCreate
上全屏输入,然后在onPause
上退出,但有时无法显示所需的结果。
我们还需要知道在较低的API上显示全屏片段的最佳方法是什么(KITKAT),在显示全屏时隐藏工具栏的最佳方法是什么,因为我们的代码((AppCompatActivity)Objects.requireNonNull(getActivity())).getSupportActionBar().hide();
有时会抛出nullpointerexception。
我们的问题:
NullpointerExepection
更新:我在片段IMMERSIVE
上设置了onResume
模式,并通过这些方法将其重置为onStop
。
private void hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar().hide();
}
// Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
View decorView = getActivity().getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar().show();
}
它以全屏显示我的片段,并且隐藏了状态栏(这不是我想要的),而我的片段属于Navigation Drawer
。当我从全屏片段按返回按钮时,它在上一个片段的工具栏上显示了我的状态栏。
将工具栏放置在何处?但是我建议您将工具栏放在每个片段xml文件中,而不要在整个活动xml文件中保留一个用于整个应用程序的工具栏。See thisGoogle提供的示例应用程序,他们还在每个Fragment放置AppbarLayout和工具栏,而不是将其保留在活动布局文件中。
因此,如果将工具栏与一起放置在片段中,则可以轻松地从片段中隐藏/显示它,而不必担心会出现空指针异常。
因此您的活动xml文件将仅仅包含NavHost片段。因此整个屏幕将由当前可见的Fragment管理:)
奇巧下面的设备不支持透明状态栏功能。 Kitkat支持透明状态栏,不是透明的。因此,您可以将透明状态栏用于API级别或更高级别的棒棒糖。如果设备版本低于棒棒糖,请忽略它,因为平台不支持该功能。因此,我的其余回答假设您希望在设备版本为Lollipop或更高版本时实现透明的状态栏。
我们需要创建一个自定义NavHostFragment。这是必需的,因为在片段过渡期间,可以同时添加多个片段的视图层次结构。如果一个使用窗口插图,则另一个可能无法正确布局。要解决此问题,我们需要确保将插图分配给所有子项,而不管它们是如何消耗的。
class DispatchInsetsNavHostFragment : NavHostFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
view.setOnApplyWindowInsetsListener { v, insets ->
(v as? ViewGroup)?.forEach { child ->
child.dispatchApplyWindowInsets(insets)
}
insets
}
}
}
}
如下图所示,用Framelayout包装导航主机片段,然后放入android:fitsSystemWindows =“ true”
<FrameLayout
android:id="@+id/navHostContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<fragment
android:id="@+id/navHostFragment"
android:name="com.yourdomain.app.DispatchInsetsNavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/main" />
</FrameLayout>
在onCreate
之后,从您的MainActivity类setContentView
方法为我们上面定义的navHostContainer设置以下标志。
val navHostContainer: FrameLayout = findViewById(R.id.navHostContainer)
navHostContainer.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Make the content ViewGroup ignore insets so that it does not use the default padding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
navHostContainer.setOnApplyWindowInsetsListener(NoopWindowInsetsListener)
}
这里是NoopWindowInsetsListener
object NoopWindowInsetsListener : View.OnApplyWindowInsetsListener {
override fun onApplyWindowInsets(v: View, insets: WindowInsets): WindowInsets {
return insets
}
}
就是这样,从现在开始,您的片段对状态栏区域拥有完全所有权。您的视图将显示在透明状态栏的后面。对于某些片段,您可能不希望状态栏重叠您的内容,对于这些片段,只需将android:fitsSystemWindows =“ true”放在片段xml文件中的根布局中即可。
将app:statusBarBackground
的CoordinatorLayout
属性与android:fitsSystemWindows="true"
一起使用
就这些。编码愉快:)