【问题标题】:How to make DialogFragment width to Fill_Parent如何使 DialogFragment 宽度为 Fill_Parent
【发布时间】:2014-07-22 08:18:30
【问题描述】:

我正在开发一个 android 应用程序,我使用DialogFragment 来显示对话框,但它的宽度非常小。我怎样才能把这个宽度变成fill_parent呢?

public class AddNoteDialogFragment extends DialogFragment {

    public AddNoteDialogFragment() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getDialog().setTitle(getString(R.string.app_name));
        View view = inflater.inflate(R.layout.fragment_add_note_dialog,
                container);

        return view;
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }
}

fragment_add_note_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <EditText
        android:id="@+id/addNoteEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:hint="@string/clock_enter_add_note"
        android:imeOptions="actionDone"
        android:inputType="textCapSentences|textMultiLine"
        android:lines="5" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_button"
        android:text="@string/submit_button"
        android:textColor="@android:color/white" />

</LinearLayout>

【问题讨论】:

  • 实际上,使用RelativeLayout 完全相同的布局显示得非常好。不过我不解释...

标签: android android-layout android-fragments android-dialogfragment


【解决方案1】:

这是我想出的解决这个问题的解决方案:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);    
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();

    Dialog dialog = getDialog();
    if (dialog != null) {
       dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
       dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }
}

编辑:

在返回膨胀视图之前,您可以在 Fragment 的onCrateView 方法中使用下面的代码。

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

【讨论】:

  • 你可以拨打dialog.getWindow().setBackgroundDrawable(null);
  • 如果设置为null,则不透明...只有黑色,在4.0.4上
  • 对我来说,如果我将 getDialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 放在 DialogFragment 的 onResume() 方法中,它就可以工作。它不适用于 onCreateView() 方法。我将答案从dialog稍微修改为getDialog()
  • onStart 方法中的 setLayout 效果很好。谢谢。
【解决方案2】:

您是否尝试过使用来自How to make an alert dialog fill 90% of screen size 的猫王的答案?

如下:

dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

更新:

上面的代码应该加在DialogFragmentonStart()方法里面。

【讨论】:

  • 您导入了哪些 LayoutParams?
  • @ymerdrengene 实际上很好的问题。试试WindowManager.LayoutParams.FILL_PARENT
  • @ymerdrengene 虽然从技术上讲,它们都只是从ViewGroup.LayoutParams 继承了这个值,所以无论哪种方式都可以正常工作。
  • 正如这里提到的stackoverflow.com/a/15279400/1641882 我建议将此代码移至onCreateDialog
  • 导入android.view.WindowManager;
【解决方案3】:

这对我有用

创建您的自定义样式:

   <style name="DialogStyle" parent="Base.Theme.AppCompat.Dialog">
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
   </style>

您也可以尝试使用正确的父级来匹配您的其他对话框。比如parent="ThemeOverlay.AppCompat.Dialog"等等。

在你的对话框中使用这种风格

public class MessageDialog extends DialogFragment {

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

// your code 
}

【讨论】:

  • 带有 ​​@null 的漂亮解决方案。
  • 注意使用正确的父级来匹配您的其他对话框。就我而言,它是 ThemeOverlay.AppCompat.Dialog
【解决方案4】:

对我来说,当我将在 onCreateView 中膨胀的布局的 LinearLayout 父级替换为 RelativeLayout 时,它就起作用了。无需更改其他代码。

【讨论】:

  • 典型的,总是最不受欢迎的堆栈溢出答案解决了我的问题。谢谢
  • 我非常喜欢这个答案,但不知道为什么它只适用于相对布局?
【解决方案5】:
    <!-- A blank view to force the layout to be full-width -->
    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_gravity="fill_horizontal" />

在我的对话框布局的顶部做到了这一点。

【讨论】:

  • 这对我有用,我喜欢它不需要我们在代码中添加任何额外的东西,但我想知道为什么它有效...跨度>
  • 难以置信,这是唯一对我有用的东西。自定义样式没有。
  • 完美。需要像这样简单的东西。
【解决方案6】:
public class FullWidthDialogFragment extends AppCompatDialogFragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.Theme_AppCompat_Light_Dialog_Alert);
    }
}

如果您想要更大的灵活性,可以扩展 AppCompat_Dialog_Alert 和自定义属性

【讨论】:

    【解决方案7】:

    基于其他解决方案,我创建了自己的解决方案。

    <style name="AppTheme.Dialog.Custom" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowMinWidthMajor">100%</item>
        <item name="android:windowMinWidthMinor">100%</item>
    </style>
    
    abstract class BaseDialogFragment : DialogFragment() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setStyle(DialogFragment.STYLE_NO_TITLE, R.style.AppTheme_Dialog_Custom)
        }
    }
    

    【讨论】:

      【解决方案8】:

      就我而言,我还使用了以下方法:

          @Override
          public void onStart() {
              super.onStart();
              getDialog().getWindow().setLayout(LayoutParams.MATCH_PARENT, (int) getResources().getDimension(R.dimen.dialog_height));
          }
      }
      

      但在某些 Lollipop+ 设备(例如 Nexus 9)上,对话框的左右边缘与屏幕边缘之间仍然存在小间隙

      这不是很明显,但最后我发现要使其 全宽 跨越所有设备和平台 窗口背景 应该在 styles.xml 中指定 如下:

      <style name="Dialog.NoTitle" parent="Theme.AppCompat.Dialog">
          <item name="android:windowNoTitle">true</item>
          <item name="android:padding">0dp</item>
          <item name="android:windowBackground">@color/window_bg</item>
      </style>
      

      当然,当我们创建如下对话框时需要使用这种样式:

          public static DialogFragment createNoTitleDlg() {
              DialogFragment frag = new Some_Dialog_Frag();
              frag.setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog_NoTitle);
              return frag;
      }
      

      【讨论】:

      • 这应该是公认的答案!为我节省了很多时间!窗口背景起到了作用。
      【解决方案9】:

      我想澄清一下。两种方式都是正确的,但使用不同的 DialogFragment

      使用android.app.DialogFragment

      @Override
      public void onStart()
      {
          super.onStart();
          Dialog dialog = getDialog();
          if (dialog != null)
          {
              int width = ViewGroup.LayoutParams.MATCH_PARENT;
              int height = ViewGroup.LayoutParams.MATCH_PARENT;
              dialog.getWindow().setLayout(width, height);
          }
      }
      

      使用android.support.v4.app.DialogFragment

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

      【讨论】:

      • 基本上我需要全宽,这很有效。谢谢!
      【解决方案10】:

      这非常适合我。

      android:minWidth="300dp"
      

      通过这个你可以给对话框片段的宽度。

      【讨论】:

        【解决方案11】:

        DialogFragment 中的用户 AlertDialog.Builder。像这样将它添加到onCreateDialog 方法中。在onCreateView 中什么也不做。

        public Dialog onCreateDialog(Bundle savedInstanceState)
        {
        
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            View view = LayoutInflater.from(getContext()).inflate(R.layout.gf_confirm_order_timeout_dialog, null);
            final Bundle args = getArguments();
            String message = args.getString(GfConstant.EXTRA_DATA);
            TextView txtMessage = (TextView) view.findViewById(R.id.txt_message);
            txtMessage.setText(message);
        
            view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if (mListener != null)
                    {
                        mListener.onDialogConfirmOK();
                    }
                }
            });
            builder.setView(view);
            Dialog dialog = builder.create();
            dialog.setCanceledOnTouchOutside(false);
            dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            return dialog;
        }
        

        【讨论】:

          【解决方案12】:

          在您的 style.xml 文件中创建自定义样式。只需将此代码复制粘贴到您的 style.xml 文件中

          <style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
              <item name="android:windowBackground">@null</item>
              <item name="android:windowIsFloating">true</item>
          </style>
          

          然后在你的DialogFragment的createDialog方法中,通过代码创建对话框对象

           dialog = new Dialog(getActivity(), R.style.CustomDialog);
          

          这对我有用,希望对你也有帮助

          【讨论】:

          • 我必须在样式中添加以下内容以使其适合我:100% 100%
          【解决方案13】:

          当我遇到这个问题时,我就是这样解决的。试试这个。

          在你DialogFragment的onActivityCreated中,根据需要添加这段代码......

           @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                       Bundle savedInstanceState) {
                  // Inflate the layout for this fragment
                  myview=inflater.inflate(R.layout.fragment_insurance_detail, container, false);
                  intitviews();
                  return myview;
              }
              @Override
              public void onActivityCreated(Bundle arg0) {
                  super.onActivityCreated(arg0);
                  getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
                  getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                  getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                  getDialog().getWindow().setGravity(Gravity.CENTER);
              }
          

          【讨论】:

            【解决方案14】:

            选项1.在活动的oncreate中添加以下代码

             getDialog().getWindow()
                     .setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
            

            或者您可以为对话框创建自定义样式

            <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>
            
            then use that style in dialog fragment
            
            @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");
            

            或者你试试下面的代码风格会帮助你

            <item name="android:windowBackground">@null</item>
            

            方案二:dialog.window.setLayout

            class TestDialog : DialogFragment() {
            
                override fun onStart() {
                    super.onStart()
            
                    dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
                }
            
            }
            

            【讨论】:

            • 只有解决方案 2 有效 (dialog.window.setLayout)
            【解决方案15】:

            在DialogFragment的onCreateView方法中使用这个

                Display display = getActivity().getWindowManager().getDefaultDisplay();
                int width = display.getWidth();
                int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **220**, getResources().getDisplayMetrics());
                getDialog().getWindow().setLayout(width,px);
            

            220 是对话框片段高度,随意更改

            【讨论】:

              【解决方案16】:

              在您的布局根目录中,将 android:minWidth 设置为一个非常大的值 例如

              <LinearLayout
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:minWidth="9999999dp">
              
                 ...
              
              </LinearLayout>
              

              【讨论】:

                【解决方案17】:

                我尝试了上面列出的多个答案;他们没有为我工作。 这是我的问题的解决方案:

                DialogFragment()中,添加以下代码:

                override fun onResume() {
                    super.onResume()
                    if (showsDialog) {
                        val width = ViewGroup.LayoutParams.MATCH_PARENT
                        val height = ViewGroup.LayoutParams.WRAP_CONTENT
                        dialog?.window?.apply {
                            setBackgroundDrawable(ColorDrawable(Color.WHITE))
                            attributes.gravity = Gravity.BOTTOM
                            setLayout(width, height)
                        }
                    }
                }
                

                【讨论】:

                  【解决方案18】:

                  一种更清晰、更灵活的方法是设置屏幕宽度的百分比。可以对高度做同样的事情,而且很容易改变。

                  override fun onResume() {
                      super.onResume()
                      dialog?.window?.let { window ->
                          val params: ViewGroup.LayoutParams = window.attributes
                          params.width = context?.getScreenSize(false)?.x?.times(0.9)?.toInt()
                              ?: ViewGroup.LayoutParams.MATCH_PARENT
                          params.height = ViewGroup.LayoutParams.WRAP_CONTENT
                          window.attributes = params as WindowManager.LayoutParams
                      }
                  }
                  

                  【讨论】:

                    【解决方案19】:

                    如果ConstraintLayout 被用作根布局,它会变成全宽,没有任何额外的代码。

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                    
                    </androidx.constraintlayout.widget.ConstraintLayout>
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2013-10-26
                      • 1970-01-01
                      • 2011-09-22
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多