在 DialogFragment 中为 AlertDialog 膨胀自定义视图的问题 [英] Problem inflating custom view for AlertDialog in DialogFragment
问题描述
我正在尝试使用 AlertDialog
中的自定义视图创建一个 DialogFragment
.这个视图必须从 xml 膨胀.在我的 DialogFragment
类中,我有:
I'm trying to create a DialogFragment
using a custom view in an AlertDialog
. This view must be inflated from xml. In my DialogFragment
class I have:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, null))
.setPositiveButton(android.R.string.ok, this)
.setNegativeButton(android.R.string.cancel, null)
.create();
}
我已经为 .setView()
尝试了其他膨胀方法,例如:
I have tried other inflation methods for .setView()
such as:
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, (ViewGroup) getView(), false))
和
.setView(getActivity().getLayoutInflater().inflate(R.layout.dialog, (ViewGroup) getTargetFragment().getView(), false))
在显示此对话框的片段中设置目标片段后.
After setting the target fragment in the fragment that is showing this dialog.
所有这些试图夸大我的自定义视图的尝试都会导致以下异常:
All of these attempts to inflate my custom view result in the following exception:
E/AndroidRuntime(32352): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/AndroidRuntime(32352): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:214)
E/AndroidRuntime(32352): at com.android.internal.app.AlertController.installContent(AlertController.java:248)
E/AndroidRuntime(32352): at android.app.AlertDialog.onCreate(AlertDialog.java:314)
E/AndroidRuntime(32352): at android.app.Dialog.dispatchOnCreate(Dialog.java:335)
E/AndroidRuntime(32352): at android.app.Dialog.show(Dialog.java:248)
E/AndroidRuntime(32352): at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:339)
E/AndroidRuntime(32352): at android.support.v4.app.Fragment.performStart(Fragment.java:1288)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:873)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
E/AndroidRuntime(32352): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:625)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1360)
E/AndroidRuntime(32352): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:411)
E/AndroidRuntime(32352): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(32352): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(32352): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime(32352): at android.app.ActivityThread.main(ActivityThread.java:4028)
E/AndroidRuntime(32352): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(32352): at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(32352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
E/AndroidRuntime(32352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
E/AndroidRuntime(32352): at dalvik.system.NativeStart.main(Native Method)
如果我尝试像这样使用 DialogFragment
的 getLayoutInflator(Bundle)
:
While if I try to use the DialogFragment
's getLayoutInflator(Bundle)
like this:
.setView(getLayoutInflater(savedInstanceState).inflate(R.layout.dialog, null))
我收到一个StackOverflowError
.
有谁知道如何在 DialogFragment
中为 AlertDialog
增加自定义视图?
Does anyone know how to inflate a custom view for an AlertDialog
in a DialogFragment
?
推荐答案
第一行错误提示我这与您创建对话框的方式有关 - 而不是对话框本身.
The first error line gives me the hint that this is related to how you are creating your dialog - not the dialog itself.
您是自动创建对话框(这可能意味着在所有视图设置之前调用它)还是响应按钮单击?由于实例化顺序,我最初遇到了片段问题.
Are you creating the dialog automatically (which could mean this gets called before the views are all set up) or in response to a button click? I initially had problems with fragments due to instantiation order.
我使用与您相同的代码来设置视图,并且我的结果有效.我删掉了其他设置以使它看起来更简洁,但无论有没有它都可以使用.
I used the same code to set the view as you have, and my result works. I cut out the other setup to make this look cleaner, but it works with or without it.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_layout, null);
builder.setView(view);
return builder.create();
}
这篇关于在 DialogFragment 中为 AlertDialog 膨胀自定义视图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!