我终于找到了解决我的问题的方法。
对话框的创建:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
if (!hasTitle()) {
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}
if (position != null) {
WindowManager.LayoutParams windowParams = dialog.getWindow().getAttributes();
windowParams.x = position.x;
windowParams.y = position.y;
dialog.getWindow().setAttributes(windowParams);
} else {
WindowManager.LayoutParams attrs = dialog.getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(hasTitle() ? Color.WHITE : Color.TRANSPARENT));
dialog.getWindow().setGravity(Gravity.TOP | Gravity.START);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
return dialog;
}
片段的创建(我自定义对话框的样式 - 使用No_FRAME非常重要):
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We customize the style of this dialog - we remove the frames of the dialogs and leave the drawing to the
// onCreateView() method, and we use the custom theme for dialogs - this is a special theme which has no
// Background and the status bar is left unchanged
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.Theme_Dialog);
}
自定义主题(我重复使用所有样式,以便对话框看起来相同):
<!-- Custom theme for dialogs - this will use the same styling as the main theme, but will disable the background
of the window, and it will also leave the status bar color unchanged -->
<style name="Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<!-- We do not want to change the status bar color -->
<item name="colorPrimaryDark">@color/transparent</item>
</style>
现在,我的对话框片段看起来像是全屏的,并且在打开软键盘时不再调整大小,因此在手动更改其大小时会产生闪烁效果。
最重要的是DialogFragment.STYLE_NO_FRAME
.
我希望这能帮助其他有类似问题的人。