【问题标题】:Android Creating a Custom AlertDialog in DialogFragmentAndroid 在 DialogFragment 中创建自定义 AlertDialog
【发布时间】:2013-05-19 21:50:19
【问题描述】:

我正在尝试创建一个 AlertDialog,其中包含一个 Next 和一个 Close 按钮​​以及一个“不再显示”复选框。我使用 DialogFragment 的支持库。以下代码可以正常工作,但我想为此 AlertDialog 使用我自己的 xml 布局:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("num");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                builder.setTitle("ASDASDAS")
                .setPositiveButton(R.string.hello_world,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((MainActivity)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((MainActivity)getActivity()).doNegativeClick();
                        }
                    }
                );
                return builder.create();
    }

是否可以使用我自己的 xml 布局来创建这个 AlertDialog?

【问题讨论】:

标签: android android-alertdialog android-dialogfragment


【解决方案1】:

这是如何使用您自己的 xml 布局在 DialogFragment 中创建完全自定义的 AlertDialog。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();

【讨论】:

    【解决方案2】:

    【讨论】:

    • 是的,我终于找到了它,但首先我们必须像 Marko 指出的那样放大视图。我把完整的答案写在下面,谢谢
    【解决方案3】:

    你可以像这样使用对话框:

    private void showIconsDlg(final int btnId) {
                // Use a custom style: IconsDialog
        final Dialog dlg = new Dialog(mContext, R.style.IconsDialog);
                // Use a custom layout: 
        dlg.setContentView(R.layout.your_custom_dlg);
    
        // Find and init Views
        GridView grid = (GridView) dlg.findViewById(R.id.icon_grid);
    
    
        grid.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                                ...
    
            }
        });
    
        dlg.show();
    
    }
    

    这是主题:IconsDialog:

    <style name="IconsDialog" parent="@android:style/Theme.Dialog">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowBackground">@drawable/icons_dlg_bg</item>
        <item name="android:windowNoTitle">true</item>
    </style>
    

    在 R.layout.your_custom_dlg 中设置您的自定义警报布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-02-04
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      相关资源
      最近更新 更多