【问题标题】:Dialog fragment not occupying complete screen对话框片段不占用整个屏幕
【发布时间】:2017-03-03 05:53:29
【问题描述】:

我正在打开对话框片段。但是,它不会占据整个屏幕。我的意思是它会留下一些空间。

下面是我的代码:

    public class MyDialogFragment extends DialogFragment implements View.OnClickListener{
        private Context context = getActivity();

        private ImageView imageViewCancel;
        private TextView textViewTitle;
        private View view;

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

            getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

            textViewTitle = (TextView) view.findViewById(R.id.textView_Screen_Title);
            setHeader("Some title");
            imageViewCancel = (ImageView) view.findViewById(R.id.imageView_Cancel);
            imageViewCancel.setOnClickListener(this);


            //to hide keyboard when showing dialog fragment
            getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

            return view;
        }

        private void setHeader(String screenTitle) {
            textViewTitle.setText(screenTitle);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.imageView_Cancel :
                    dismiss();
                    break;
            }
        }

        @Override
        public void onStart() {
            super.onStart();
getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        }
    }

参考快照

我想以全屏模式打开它,这样就不会看到背景透明。 WindowManager.LayoutParams.MATCH_PARENT & ViewGroup.LayoutParams.MATCH_PARENT 也试过了,但没有任何效果。

【问题讨论】:

标签: android android-fragments android-dialogfragment dialogfragment android-layoutparams


【解决方案1】:

Dialog创建自定义样式

<style name="CustomDialog" parent="AppTheme" >
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowIsFloating">true</item>
  <item name="android:windowCloseOnTouchOutside">true</item>
</style>

然后在对话框片段中使用该样式

@Override public void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
}

@Override public void onStart() {
  super.onStart();
  getDialog().getWindow()
    .setLayout(WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT);
}

SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
SampleDialogFragment.show(getActivity().getSupportFragmentManager(), "sometag");

【讨论】:

  • 告诉我你遇到的问题
  • 仅保留 OR 部分...以上 1 不适用于其他设备
【解决方案2】:

试试这个: 在styles.xml中添加这个

 <style name="MY.DIALOG" parent="android:Theme" >
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

并在您的 DialogFragment 中设置此样式:

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.MY_DIALOG);
}

对话框片段的背景会显示为黑色,根据需要更改。

请参阅此帖子了解更多信息: http://www.techrepublic.com/article/pro-tip-unravel-the-mystery-of-androids-full-screen-dialog-fragments/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2016-10-22
    • 2021-03-02
    • 2019-08-05
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多