Android:不显示具有自定义布局的DialogFragment [英] Android: support DialogFragment with custom layout not shown
问题描述
我的应用程序中有一个DialogFragment
(android.support.v4.app.DialogFragment
)的子类,可以长时间正常工作.对话框本身是在onCreateDialog()
回调中构建的.
I've had a subclass of DialogFragment
(android.support.v4.app.DialogFragment
) in my app that worked fine for a long time. The dialog itself was constructed in onCreateDialog()
callback.
但是,现在,我想过渡到新的对话框布局,并决定构建一个完全自定义的对话框.据我所知,这需要删除onCreateDialog()
的覆盖并在onCreateView()
回调中扩大View
层次结构.我是这样做的.
Now, however, I want to transition to a new dialogs layout and decided to construct a completely customized dialog. To my best knowledge, this requires removing override of onCreateDialog()
and inflating a View
hierarchy in onCreateView()
callback. I did so.
由此产生的行为非常奇怪-当需要显示对话框时,屏幕变暗,但是未显示对话框的布局(后退"按钮的行为也与对话框的功能一致):
The resulting behavior is very strange - when the dialog needs to be shown, the screen dims, but the layout of the dialog is not shown (the behavior of "back" button is also consistent with dialog's functionality):
我试图恢复到旧的实现(它使用onCreateDialog()
回调)-仍然有效.
I tried to revert back to the old implementation (which uses onCreateDialog()
callback) - it still works.
我的问题是:我在做什么错了?
My question is: what am I doing wrong?
对话框的代码:
/**
* 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>
推荐答案
我最终得到了这种解决方法,但是我仍然想了解WTF:
I ended up with this workaround, but I'd still want to understand 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);
}
这篇关于Android:不显示具有自定义布局的DialogFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!