DialogFragment被状态栏遮挡了

8
我有点困惑。
起初,当我创建一个工具栏并且它被状态栏重叠时,我只需在父级XML中添加fitSystemWindow="true",这样就可以正常工作了。
但是,当我创建全屏DialogFragment时,它也被状态栏重叠。我尝试添加fitSystemWindow="true",但它不起作用。
仅适用于Android 5.0+。没有在任何地方将状态栏设置为半透明。
这是我的DialogFragment代码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_create_bill_dialog,container, false);

    return view;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

谢谢。很抱歉我的英语不好。

3个回答

1

我遇到了同样的问题。我通过在Dialog Fragment根视图顶部添加25dp的填充来解决它,以确保它不会与状态栏重叠。

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //If showing as a dialog, add some padding at the top to ensure it doesn't overlap status bar
    if (getDialog() != null) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        view.setPadding(0, (int)PresentationUtils.convertDpToPixel(25f, getContext()), 0, 0);
        view.requestLayout();
    }
...
}

顺便提一下,我正在使用的将dp转换为px的工具方法可以在这里找到 将像素转换为dp


1
很不幸,这个解决方案不好,因为状态栏的大小因设备而异,所以这是一个非常糟糕的做法(请参见https://youtu.be/_mGDMVRO3iE?t=1074)。 - Kiryl Tkach

0

这可能会有所帮助:

View decorView = getWindow().getDecorView();

// 隐藏状态栏。

int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;

decorView.setSystemUiVisibility(uiOptions);

// 记住,如果状态栏被隐藏了,就不应该显示操作栏,所以必要时也要将其隐藏。

ActionBar actionBar = getActionBar();

actionBar.hide();

以上代码禁用了4.1及以上版本的设备状态栏


2
谢谢,但有没有办法在不隐藏状态栏的情况下解决这个问题。 - Math Burn

0

在你的样式中:

<style name="MyDialog" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>

在你的对话框中:

new Dialog(builder.context, R.style.MyDialog);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接