如何使DialogFragment宽度为Fill_Parent

时间:2014-06-02 08:56:54

标签: android android-fragments fragment android-dialogfragment

您好我正在使用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>

enter image description here

提前致谢。

20 个答案:

答案 0 :(得分:122)

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

@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));
    }
}

编辑:

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

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

答案 1 :(得分:63)

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

以下是:

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

<强>更新

上面的代码应添加到onStart()。{/ p>的DialogFragment方法中

LayoutParams.FILL_PARENT已被弃用,而是使用LayoutParams.MATCH_PARENT

答案 2 :(得分:37)

这对我有用

创建自定义样式:

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

在对话框中使用此样式

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 
}

答案 3 :(得分:20)

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

答案 4 :(得分:17)

    <!-- 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" />

在对话框布局的顶部做了诀窍。

答案 5 :(得分:13)

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和自定义属性

答案 6 :(得分:10)

在我的情况下,我也使用了以下方法:

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

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

这一点并不明显,但最后我发现要在所有设备和平台上实现全宽 window background 应在 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;
}

答案 7 :(得分:4)

这完全适合我。

android:minWidth="300dp"

通过此操作,您可以为对话框片段指定宽度。

答案 8 :(得分:3)

我想澄清一下。两种方法都是正确的,但是使用不同的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);
}

答案 9 :(得分:2)

另一种对我没有副作用的解决方案是:

在您的类中扩展DialogFragment:

<style name="DialogStyle" parent="ThemeOverlay.AppCompat.Dialog.Alert"></style>

在你的风格中:

app

答案 10 :(得分:1)

在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);

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

答案 11 :(得分:0)

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

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
    }
}

答案 12 :(得分:0)

选项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>

解决方案2:dialog.window.setLayout

class TestDialog : DialogFragment() {

    override fun onStart() {
        super.onStart()

        dialog?.window?.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
    }

}

答案 13 :(得分:0)

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

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)
        }
    }
}

答案 14 :(得分:0)

这是我遇到此问题时所解决的方法。试试这个。

在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);
    }

答案 15 :(得分:0)

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

<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)
    }
}

答案 16 :(得分:0)

在你的布局根目录中,将android:minWidth设置为一个非常大的值 e.g

  This is my web.xml file inside WEB-INF/ .classes mapped inside web.xml is in WEB-INF/classes.All class files inside this is getting erased.

   <?xml version="1.0" encoding="ISO-8859-1"?>


   <web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
   <description>
    Welcome to Tomcat
   </description>
   <servlet>
   <servlet-name>HelloForm</servlet-name>
   <servlet-class>HelloForm</servlet-class>
   </servlet>

   <servlet-mapping>
   <servlet-name>HelloForm</servlet-name>
   <url-pattern>/HelloForm</url-pattern>
   </servlet-mapping>
   <servlet>
   <servlet-name>Upload</servlet-name>
   <servlet-class>Upload</servlet-class>
   </servlet>

  <servlet-mapping>
   <servlet-name>Upload</servlet-name>
   <url-pattern>/Upload</url-pattern>
  </servlet-mapping>
 <servlet>
 <servlet-name>Getapplicants</servlet-name>
 <servlet-class>Getapplicants</servlet-class>
</servlet>

  <servlet-mapping>
   <servlet-name>Getapplicants</servlet-name>
<url-pattern>/Getapplicants</url-pattern>
 </servlet-mapping>
 <servlet>
    <servlet-name>First_round_selected</servlet-name>
    <servlet-class>First_round_selected</servlet-class>
  </servlet>

  <servlet-mapping>
   <servlet-name>First_round_selected</servlet-name>
   <url-pattern>/First_round_selected</url-pattern>
   </servlet-mapping>

  <servlet>
  <servlet-name>Second_round_selected</servlet-name>
  <servlet-class>Second_round_selected</servlet-class>
 </servlet>

   <servlet-mapping>
  <servlet-name>Second_round_selected</servlet-name>
   <url-pattern>/Second_round_selected</url-pattern>
   </servlet-mapping>



   </web-app>

答案 17 :(得分:0)

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;
}

答案 18 :(得分:0)

在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 是对话框片段高度,根据需要更改

答案 19 :(得分:-1)

如果将ConstraintLayout用作根布局,它将变为全角,而无需任何其他代码。

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

</androidx.constraintlayout.widget.ConstraintLayout>