显示DialogFragment时隐藏系统导航栏

时间:2019-07-11 09:30:34

标签: java android

我的应用位于sticky immersive mode中,system navigation bar(我称为SNB)在我的应用的运行时间中被隐藏。它运作良好,但存在一个问题,那就是每次我显示DialogFragment时,SNB都会出现。我怎么藏起来

我在acivity类中写了几行代码,以使我的应用全屏显示并sticky immersive mode

  

MyActivity.java

@Override
protected void onResume() {
    super.onResume();
    hideSystemUI();
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI();
}

public void hideSystemUI() {
    // Enables regular immersive sticky mode.
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    );
}
  

MyDialog.java

public class MyDialog extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        AlertDialog alertD = builder.create();
        alertD.setView(view);
        return alertD;
    }

    public void show() {
        Activity activity = getActivity();
        show(activity.getSupportFragmentManager(), "dialog");
    }

    @Override
    public void onResume() {
        super.onResume();
        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);
    }
}

我按照回答进行了显示,但没有显示SNB的对话框:https://stackoverflow.com/a/33589793/10467639,但是它没有按要求工作

MyDialog dialog = new MyDialog();
// Set the dialog to not focusable.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.show();
// Set the dialog to focusable again.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
  

style.xml   

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/themeColor</item>
        <item name="colorPrimaryDark">@color/themeColor</item>
        <item name="colorAccent">@color/themeColor</item>
        <item name="colorControlNormal">@color/black</item>
        <item name="colorControlActivated">@color/black</item>
        <item name="colorControlHighlight">@color/white</item>
    </style>
    <style name="BoldGrayHeaderTextView">
        <item name="android:textSize">12dp</item>
        <item name="android:fontFamily">@font/nunitosans_bold</item>
        <item name="android:textColor">@color/darkColor</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <!-- For CarPinItem on Map -->
    <style name="carPinItemUnSelected">
        <item name="android:textSize">12dp</item>
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textColor">@color/generalTextColor</item>
    </style>

    <style name="carPinItemSelected">
        <item name="android:textSize">12dp</item>
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textColor">@color/white</item>
    </style>

    <!-- Style for drawer navigation item                                                                       -->
    <style name="NavigationDrawerStyle">
        <item name="android:textSize">15dp</item><!-- text size in menu-->
        <item name="android:listPreferredItemHeightSmall">40dp</item><!-- item size in menu-->
        <item name="listPreferredItemHeightSmall">40dp</item><!-- item size in menu-->
    </style>

    <!--Style for textInputEditText   -->
    <style name="TextInputLayoutStyle">
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textSize">13dp</item>
        <item name="android:textColor">@color/white</item>

    </style>
    <!--Style for profile bottom menu   -->
    <style name="BottomTextLayoutStyle">
        <item name="android:fontFamily">@font/nunitosans_regular</item>
        <item name="android:textColor">@color/black</item>
    </style>

    <style name="MyDatePickerStyle" parent="@android:style/Widget.Material.Light.DatePicker">
        <item name="android:textColorPrimaryInverse">@color/black</item>
        <item name="android:colorControlActivated">@color/successColor</item>
    </style>

    <style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorControlNormal">@color/white</item>
        <item name="colorControlActivated">@color/white</item>
        <item name="colorControlHighlight">@color/white</item>

    </style>

    <!--BottomNavivagionView textsize-->
    <style name="Widget.BottomNavigationView"
        parent="Widget.Design.BottomNavigationView">
        <item name="android:fontFamily">@font/nunitosans_regular</item>
    </style>

    <style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
    </style>
</resources>

如何解决此问题?在此先感谢!

2 个答案:

答案 0 :(得分:1)

按照这个答案https://stackoverflow.com/a/23207365/8531215,我已经测试过,并且效果很好!稍微修改onCreateDialog()方法

public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        AlertDialog alertD = builder.create();
        alertD.setView(view);
        Dialog dialog = alertD.create();
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        //Show the dialog!
        dialog.show();

         //Set the dialog to immersive sticky mode
        dialog.getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        //Clear the not focusable flag from the window
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        return alertD;
}

答案 1 :(得分:0)

根据文档here

  

实现onWindowFocusChanged()。如果您获得窗口焦点,则可能需要重新隐藏系统栏。如果您失去窗口焦点(例如,由于在应用程序上方显示对话框或弹出菜单),则可能要取消以前使用Handler.postDelayed()或类似方法安排的所有暂挂“隐藏”操作。

因此,我建议您从以下位置更改MyActivity.java中的代码:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    hideSystemUI();
}

收件人:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
     if (!hasFocus){
            //we lost focus due to DialogFragment
            hideSystemUI();            
        }
}