1
我的應用程序中有一個DialogFragment
(android.support.v4.app.DialogFragment
)的子類,它很長一段時間都運行良好。該對話框本身是在onCreateDialog()
回調中構建的。Android:支持DialogFragment,自定義佈局未顯示
但是,現在我想轉換到新的對話框佈局,並決定構建一個完全自定義的對話框。據我所知,這需要刪除覆蓋onCreateDialog()
和回調的View
層次結構。我這樣做了。
產生的行爲是非常奇怪 - 當對話框需要顯示時,屏幕會變暗,但該對話框的佈局不顯示(「後退」按鈕的行爲也符合對話的功能):
我試圖恢復到舊的實現(它使用onCreateDialog()
回調) - 它仍然有效。
我的問題是:我做錯了什麼?
Dialog的代碼:
/**
* 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>
大規模煩人問題 - 這看起來確實是一個錯誤。 – cfl