Android:支持自定义布局未显示的DialogFragment

时间:2017-01-18 22:32:34

标签: android android-dialogfragment dialogfragment

我的应用程序中有一个DialogFragmentandroid.support.v4.app.DialogFragment)的子类,它在很长一段时间内都运行良好。对话框本身是在onCreateDialog()回调中构建的。

但是,现在,我想转换到新的对话框布局,并决定构建一个完全自定义的对话框。据我所知,这需要删除onCreateDialog()的覆盖并在View回调中夸大onCreateView()层次结构。我这样做了。

产生的行为非常奇怪 - 当需要显示对话框时,屏幕变暗,但对话框的布局未显示("后退"按钮的行为也与对话框一致&# 39; s功能):

enter image description here

我试图恢复旧的实现(使用onCreateDialog()回调) - 它仍然有效。

我的问题是:我做错了什么?

对话框的代码:

/**
 * A dialog that can show title and message and has two buttons. User's actions performed
 * in this dialog will be posted to {@link EventBus} as {@link PromptDialogDismissedEvent}.
 */
public class PromptDialog extends BaseDialog {


    /* package */ static final String ARG_TITLE = "ARG_TITLE";
    /* package */ static final String ARG_MESSAGE = "ARG_MESSAGE";
    /* package */ static final String ARG_POSITIVE_BUTTON_CAPTION = "ARG_POSITIVE_BUTTON_CAPTION";
    /* package */ static final String ARG_NEGATIVE_BUTTON_CAPTION = "ARG_NEGATIVE_BUTTON_CAPTION";

    @Inject EventBus mEventBus;

    private TextView mTxtTitle;
    private TextView mTxtMessage;
    private Button mBtnPositive;
    private Button mBtnNegative;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        getControllerComponent().inject(this);
    }

    // THIS CODE MAKES THE SCREEN DIM, BUT THE ACTUAL LAYOUT IS NOT SHOWN

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.dialog_info_prompt, container, false);

        initSubViews(rootView);

        populateSubViews();

        return rootView;

    }

    private void initSubViews(View rootView) {
        mTxtTitle = (TextView) rootView.findViewById(R.id.txt_dialog_title);
        mTxtMessage = (TextView) rootView.findViewById(R.id.txt_dialog_message);
        mBtnPositive = (Button) rootView.findViewById(R.id.btn_dialog_positive);
        mBtnNegative = (Button) rootView.findViewById(R.id.btn_dialog_negative);

        mBtnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE));
            }
        });

        mBtnNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE));
            }
        });
    }

    private void populateSubViews() {
        String title = getArguments().getString(ARG_TITLE);
        String message = getArguments().getString(ARG_MESSAGE);
        String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION);
        String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION);

        mTxtTitle.setText(TextUtils.isEmpty(title) ? "" : title);
        mTxtMessage.setText(TextUtils.isEmpty(message) ? "" : message);
        mBtnPositive.setText(positiveButtonCaption);
        mBtnNegative.setText(negativeButtonCaption);
    }


    // THE BELOW CODE WORKS FINE (the dialog is shown and responsive)

//    @Override
//    public @NonNull Dialog onCreateDialog(Bundle savedInstanceState) {
//        String title = getArguments().getString(ARG_TITLE);
//        String message = getArguments().getString(ARG_MESSAGE);
//        String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION);
//        String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION);
//
//        Dialog dialog = new AlertDialog.Builder(getActivity())
//                .setTitle(TextUtils.isEmpty(title) ? "" : title)
//                .setMessage(TextUtils.isEmpty(message) ? "" : message)
//                .setPositiveButton(TextUtils.isEmpty(positiveButtonCaption) ? "" : positiveButtonCaption,
//                        new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE));
//                            }
//                        })
//                .setNegativeButton(TextUtils.isEmpty(negativeButtonCaption) ? "" : negativeButtonCaption,
//                        new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE));
//                            }
//                        })
//                .create();
//
//        return dialog;
//    }

}

自定义对话框的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:id="@+id/txt_dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_gravity="center_horizontal"
        style="@style/AppTheme.TextView.DialogTitle"
        tools:text="Dialog title"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="@color/gray"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/fragment_default_padding">

        <TextView
            android:id="@+id/txt_dialog_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            style="@style/AppTheme.TextView.DialogMessage"
            tools:text="Dialog message very very very very very very very very very very very very long"/>

        <Button
            android:id="@+id/btn_dialog_positive"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/button_height"
            android:layout_marginTop="15dp"
            android:layout_below="@id/txt_dialog_message"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            style="@style/AppTheme.GreenButton"
            tools:text="Positive"/>

        <Button
            android:id="@+id/btn_dialog_negative"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/button_height"
            android:layout_marginTop="15dp"
            android:layout_below="@id/txt_dialog_message"
            android:layout_toStartOf="@+id/btn_dialog_positive"
            android:layout_toLeftOf="@id/btn_dialog_positive"
            android:layout_marginEnd="15dp"
            android:layout_marginRight="15dp"
            style="@style/AppTheme.GrayButton"
            tools:text="Negative"/>

    </RelativeLayout>

</LinearLayout>

4 个答案:

答案 0 :(得分:2)

我最终得到了这个解决方法,但我还是想了解WTF:

    // This is a workaround for the strange behavior of onCreateView (which doesn't show dialog's layout)
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View dialogView = inflater.inflate(R.layout.dialog_info_prompt, null);
        dialogBuilder.setView(dialogView);

        initSubViews(dialogView);

        populateSubViews();

        setCancelable(false);

        return dialogBuilder.create();
    }

    private void initSubViews(View rootView) {
        mTxtTitle = (TextView) rootView.findViewById(R.id.txt_dialog_title);
        mTxtMessage = (TextView) rootView.findViewById(R.id.txt_dialog_message);
        mBtnPositive = (Button) rootView.findViewById(R.id.btn_dialog_positive);
        mBtnNegative = (Button) rootView.findViewById(R.id.btn_dialog_negative);

        mBtnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_POSITIVE));
            }
        });

        mBtnNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                mEventBus.post(new PromptDialogDismissedEvent(getDialogTag(), PromptDialogDismissedEvent.BUTTON_NEGATIVE));
            }
        });
    }

    private void populateSubViews() {
        String title = getArguments().getString(ARG_TITLE);
        String message = getArguments().getString(ARG_MESSAGE);
        String positiveButtonCaption = getArguments().getString(ARG_POSITIVE_BUTTON_CAPTION);
        String negativeButtonCaption = getArguments().getString(ARG_NEGATIVE_BUTTON_CAPTION);

        mTxtTitle.setText(TextUtils.isEmpty(title) ? "" : title);
        mTxtMessage.setText(TextUtils.isEmpty(message) ? "" : message);
        mBtnPositive.setText(positiveButtonCaption);
        mBtnNegative.setText(negativeButtonCaption);
    }

答案 1 :(得分:0)

也许有点晚,但是我偶然发现了这篇文章,因为我的应用程序中有同样的问题。我仍然不知道为什么会发生这种情况,但是我遵循了Medium article的步骤来设置DialogFragment,并且看来在onStart上更改对话框的窗口大小可能会解决此问题。

styles.xml

<style name="FullScreenDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorPrimary">@color/primary</item>

    <!-- Set this to true if you want Full Screen without status bar -->
    <item name="android:windowFullscreen">false</item>

    <item name="android:windowIsFloating">true</item>

    <!-- This is important! Don't forget to set window background -->
    <item name="android:windowBackground">@color/background_dialog</item>
</style>

DialogFragment

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

@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog == null || dialog.getWindow() == null)
        return;

    int width = 776; //Width of your dialog
    int height = 512; //Height of your dialog
    dialog.getWindow().setLayout(width, height);
}

答案 2 :(得分:0)

我添加了根布局的高度 wrap_content

答案 3 :(得分:0)

好像晚了 5 年,但我来到这里时遇到了类似的问题。与 OP 采用的解决方案类似。

我的问题是在我的 onCreate() 中,我正在创建一个 AlertDialog 并为布局充气,但缺少一步。

    public Dialog onCreateDialog(Bundle savedInstanceState){
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

     View alertView = LayoutInflater.from(getContext()).inflate(R.layout.formulario_identificar, null);
    //just loading the rest of the buttons and doing stuff
    Button button1 = alertView.findViewById(R.id.xeneroId);
     .....
     return builder.create();
    }

问题在于创建了 AlertDialog.Builder 对象,并且布局膨胀了.. 但布局没有附加到我在 builder.create() 中返回的构建器 - 换句话说,我要返回一个没有布局的构建器。

解决方案对我来说很简单:

 public Dialog onCreateDialog(Bundle savedInstanceState){
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

     View alertView = LayoutInflater.from(getContext()).inflate(R.layout.formulario_identificar, null);
    
    *****builder.setView(alertView);******

    //just loading the rest of the buttons and doing stuff
    Button button1 = alertView.findViewById(R.id.xeneroId);
     .....
     return builder.create();
    }

我将视图(膨胀布局)附加到我的 AlertDialog,所以问题解决了。