具有半透明StatusBar的全屏DialogFragment

时间:2015-02-02 18:47:05

标签: android android-5.0-lollipop statusbar android-dialogfragment

我有一个DialogFragment,我想全屏显示。但我仍然想要一个StatusBar,以及底部的硬件按钮。我还想设置StatusBar的背景颜色(对于Lollipop)。

我的问题是,如果我在DialogFragment中设置以下标志:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);   
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

StatusBar和Hardware键盘都变得半透明,DialogFragment在这些后面延伸。

以下是代码,它已经大大减少,变得可读:

public class CardDetailsDialog extends DialogFragment {

Setup parameters...

public static CardDetailsDialog newInstance(final long cardId, final long projectId){
    CardDetailsDialog frag = new CardDetailsDialog();
    frag.setStyle(DialogFragment.STYLE_NORMAL, R.style.CardDetailsDialogStyle);
    return frag;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(getDialog() != null) {
        getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogSlideAnimation;
        getDialog().getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        getDialog().getWindow().setStatusBarColor(Color.RED);
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.card_details, container, false);

    Handle everything that happens inside the view...

    return view;
}
}

以下是推荐的主题:

<style name="CardDetailsDialogStyle" parent="@style/Theme.AppCompat.Light.Dialog" >
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

片段的风格:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pp.whiteBackgroundColor" >

<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_details_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_alignParentTop="true"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/PopupMenutheme">
</android.support.v7.widget.Toolbar>

    <ScrollView
        android:id="@+id/details_scrollview"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        All subview elements here...

    </ScrollView>

</RelativeLayout>

结果如下:Screenshot

如您所见,ToolBar扩展了StatusBar和硬件按钮。我不知道我是否正确接近这个。我错过了什么吗?

修改

当我删除

时,这就是相同的视图
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

enter image description here

5 个答案:

答案 0 :(得分:6)

尝试使用您应用中的相同样式。我用简单的对话框测试没有片段,工作正常。 像那样:

new Dialog(context, R.style.CardDetailsDialogStyle);

答案 1 :(得分:4)

在我的情况下SYSTEM_UI_FLAG_LAYOUT_STABLE解决了重叠

的问题
        int width = ViewGroup.LayoutParams.MATCH_PARENT;

        int height = ViewGroup.LayoutParams.MATCH_PARENT;

        dialog.getWindow().setLayout(width,height);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            dialog.getWindow().setStatusBarColor(getResources().getColor(R.color.darkGrayTransp));
            dialog.getWindow().getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_STABLE);//solves issue with statusbar
            dialog.getWindow().setGravity(Gravity.CENTER_HORIZONTAL| Gravity.TOP);
        }

答案 2 :(得分:3)

对于仍有此问题的人,请执行以下操作。这只是解决了 发布的问题的一半,即黑色状态栏

将以下主题添加到 res / value-v21 / style

<style name="DialogTheme" parent="@style/Base.Theme.AppCompat.Light.Dialog">
     <item name="android:windowTranslucentStatus">true</item>
</style>

然后在DialogFragment

中的onCreate上应用样式
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogTheme);
}

<强> 修改 如果您对话框主题出现问题,请使用此样式,例如colorAccentcolorControlHighlight

<style name="DialogTheme" parent="@style/ThemeOverlay.AppCompat.Dialog">
     <item name="android:windowTranslucentStatus">true</item>
</style>

enter image description here

答案 3 :(得分:2)

你必须设置fitsystemwindows = true。另一种方法是在对话框显示时添加一个带0dp的空格并将其高度更改为25dp。

要更改空间大小,请使用布局参数,请查看此帖子:How to create a RelativeLayout programmatically with two buttons one on top of the other?

答案 4 :(得分:0)

<style name="DialogTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsFloating">false</item>
    </style>