DialogFragment onCreateView 不返回自定义布局视图?
DialogFragment onCreateView not returning custom layout view?
我目前正在尝试对我的 DialogFragment 使用自定义布局,但似乎我做错了什么。这是我的 class 目前的样子:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true);
Log.d(TAG, "onCreateDialog");
return new AlertDialog.Builder(getActivity())
.setPositiveButton(android.R.string.ok, passDataListener)
.setNegativeButton(android.R.string.cancel, null)
.setCancelable(true)
.create();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
setTitleFromBundle();
return inflater.inflate(R.layout.dialog_edittext, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mEditText = (EditText) view.findViewById(R.id.dialog_edittext);
tvUnits = (TextView) view.findViewById(R.id.dialog_units);
setMaxCharsFromBundle();
setTextFromBundle();
setHintFromBundle();
setInputTypeFromBundle();
setUnitsTextViewFromBundle();
}
positive/negative 按钮显示(连同标题)但是,我的布局没有显示。
您不能同时使用 onCreateDialog
和 onCreateView
这两种方法。您必须选择是否要显示 "basic dialog" 然后覆盖 onCreateDialog
。如果你想显示 "custom dialog" 然后覆盖 onCreateView
。请注意,您也可以覆盖 onCreateDialog
并使用 youralertdialog.setView()
.
设置您自己的布局
您应该只实现 onCreateView
和 onCreateDialog
之间的一种方法,而不是两者都实现。
- onCreateView : 提供对话框的内容
- onCreateDialog : 创建一个完全自定义的对话框,例如具有自己内容的 AlertDialog
一切都在文档中描述 here
如需完整指南,您可以查看 https://guides.codepath.com/android/Using-DialogFragment
不需要覆盖onCreateView()
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true);
Log.d(TAG, "onCreateDialog");
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return new AlertDialog.Builder(getActivity())
.setView(inflater.inflate(R.layout.dialog_edittext, null))
.setPositiveButton(android.R.string.ok, passDataListener)
.setNegativeButton(android.R.string.cancel, null)
.setCancelable(true)
.create();
}
我目前正在尝试对我的 DialogFragment 使用自定义布局,但似乎我做错了什么。这是我的 class 目前的样子:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true);
Log.d(TAG, "onCreateDialog");
return new AlertDialog.Builder(getActivity())
.setPositiveButton(android.R.string.ok, passDataListener)
.setNegativeButton(android.R.string.cancel, null)
.setCancelable(true)
.create();
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
setTitleFromBundle();
return inflater.inflate(R.layout.dialog_edittext, container);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mEditText = (EditText) view.findViewById(R.id.dialog_edittext);
tvUnits = (TextView) view.findViewById(R.id.dialog_units);
setMaxCharsFromBundle();
setTextFromBundle();
setHintFromBundle();
setInputTypeFromBundle();
setUnitsTextViewFromBundle();
}
positive/negative 按钮显示(连同标题)但是,我的布局没有显示。
您不能同时使用 onCreateDialog
和 onCreateView
这两种方法。您必须选择是否要显示 "basic dialog" 然后覆盖 onCreateDialog
。如果你想显示 "custom dialog" 然后覆盖 onCreateView
。请注意,您也可以覆盖 onCreateDialog
并使用 youralertdialog.setView()
.
您应该只实现 onCreateView
和 onCreateDialog
之间的一种方法,而不是两者都实现。
- onCreateView : 提供对话框的内容
- onCreateDialog : 创建一个完全自定义的对话框,例如具有自己内容的 AlertDialog
一切都在文档中描述 here
如需完整指南,您可以查看 https://guides.codepath.com/android/Using-DialogFragment
不需要覆盖onCreateView()
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setRetainInstance(true);
Log.d(TAG, "onCreateDialog");
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return new AlertDialog.Builder(getActivity())
.setView(inflater.inflate(R.layout.dialog_edittext, null))
.setPositiveButton(android.R.string.ok, passDataListener)
.setNegativeButton(android.R.string.cancel, null)
.setCancelable(true)
.create();
}