如何使 DialogFragment 宽度为 Fill_Parent [英] How to make DialogFragment width to Fill_Parent

查看:20
本文介绍了如何使 DialogFragment 宽度为 Fill_Parent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on an android application where I am using DialogFragment to display the dialog but its width is very small. How I can make this width to fill_parent to it ?

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

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>

推荐答案

这是我想出的解决方案:

This is the solution I figured out to handle this issue:

@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 方法中使用下面的代码.

You can use the code below in onCrateView method of Fragment before return the inflated view.

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

这篇关于如何使 DialogFragment 宽度为 Fill_Parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆