AlertDialog按钮的图像

Rag*_*har 10 user-interface controls android

是否可以在AlertDialog 的,中性按钮中添加drawable ?如果是,那怎么样?

aar*_*gas 14

由于onPrepareDialog不推荐使用,您可以使用onShowListener.

你也应该设置Drawable边界,或者它将被放置在最左边.

下面的代码输出

下面的代码输出

public class MyDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final AlertDialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle("My Dialog")
                .setNegativeButton("Cancel", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).setPositiveButton("Play", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                    }
                }).create();
        dialog.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface dialogInterface) {
                Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);

                // if you do the following it will be left aligned, doesn't look
                // correct
                // button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_media_play,
                // 0, 0, 0);

                Drawable drawable = getActivity().getResources().getDrawable(
                        android.R.drawable.ic_media_play);

                // set the bounds to place the drawable a bit right
                drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5),
                        0, (int) (drawable.getIntrinsicWidth() * 1.5),
                        drawable.getIntrinsicHeight());
                button.setCompoundDrawables(drawable, null, null, null);

                // could modify the placement more here if desired
                // button.setCompoundDrawablePadding();
            }
        });
        return dialog;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 7

内置后AlertDialog,onCreateDialog您可以使用以下代码onPrepareDialog将图像添加到肯定按钮:

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    AlertDialog alertDialog = (AlertDialog)dialog;
    Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(
            R.drawable.icon), null, null, null);

}
Run Code Online (Sandbox Code Playgroud)

尝试将drawable添加到onCreateDialog方法中的按钮似乎不起作用.


Sni*_*las 5

你不能在onCreateDialog中添加按钮,并且必须在onPrepareDialog中添加它,因为android会以非常特殊的方式处理AlertDialog:

当你使用警告对话框时,你实际上并没有真正持有对真实对话框的引用,你使用AlertDialog.Builder.create()获得的对象只是内部控制器的一个面.

在实际调用create之前,jvm中没有这样的控制器.只是门面.因此,在调用此方法之前(如果让您的活动管理自己的对话框,则在onCreateDialog的末尾),真实控制器不存在,而实际按钮也不存在.

全新的SOF评论者,Stéphane


sha*_*ylh 5

这可以通过使用getButton()方法获取对按钮的引用来完成:

alert.show();
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
email.setBackgroundResource(R.drawable.email);
Run Code Online (Sandbox Code Playgroud)

请注意,您必须使用getButton()AFTER调用show()方法,否则您将获得NullPointerException.