在自定义dialogfragment上添加内容之前,必须调用requestFeature()

时间:2015-03-29 20:44:38

标签: android android-dialogfragment

我正在尝试创建一个带圆角的自定义警告对话框 使用对话框片段,我read这可以实现设置 对于DialogFragment的setStyle方法,STYLE_NO_FRAME常量。 我在onCreate方法中做到了,但我得到了上面提到的RunTimeException。 关于可能出错的任何想法?

这是我的代码:

public static class AlertDialogFragment extends DialogFragment {

    private static final String DIALOG_NUMBER_KEY = "dialogNumber";
    private int dNumber;

    public static AlertDialogFragment newInstance(int dialogNumber) {
        AlertDialogFragment mDialogFragment = new AlertDialogFragment();

        // Supply dialogNumber input as an argument
        Bundle args = new Bundle();
        args.putInt(DIALOG_NUMBER_KEY, dialogNumber);
        mDialogFragment.setArguments(args);

        return mDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo);
    }

    // Build AlertDialog using AlertDialog.Builder
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        dNumber = getArguments().getInt(DIALOG_NUMBER_KEY);

        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();


        switch(dNumber) {
        case DELETE_DIALOG:
            adb.setView(inflater.inflate(R.layout.delete_record_dialog, null))
            .setCancelable(true)
            .create();
            AlertDialog customDeleteDialog = adb.show();

            Button okButton = (Button) customDeleteDialog.findViewById(R.id.ok);
            Button cancelButton = (Button) customDeleteDialog.findViewById(R.id.cancel);

            okButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // some code here
                }
            });

            cancelButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            return customDeleteDialog;
      }

2 个答案:

答案 0 :(得分:0)

您链接的示例显示调用在newInstance()方法中发生,因此在对话框实例化之后但在呈现之前调用它:

public static AlertDialogFragment newInstance(int dialogNumber) {
    AlertDialogFragment mDialogFragment = new AlertDialogFragment();

    // Supply dialogNumber input as an argument
    Bundle args = new Bundle();
    args.putInt(DIALOG_NUMBER_KEY, dialogNumber);
    mDialogFragment.setArguments(args);
    mDialogFragment.setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo);

    return mDialogFragment;
}

答案 1 :(得分:0)

我认为setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo);应该在super.onCreate(savedInstanceState);方法的onCreate 之前添加

这应该可以解决问题。