AlertDialog按钮的图像

11

是否可以在AlertDialog的“positive”,“negative”和“neutral”按钮中添加可绘制图像?如果可以,如何实现?

6个回答

16

由于onPrepareDialog已经被弃用,您可以使用onShowListener代替。

此外,您应该设置Drawable的边界,否则它将被放置在最左边。

以下是代码输出:

Output of Code below

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;
    }
}

你好,我想在播放文本旁边显示图标。你能给我一些建议吗? - Gaurav Darji
请问你能否举一个例子来说明如何使用你的代码?如何实例化对话框? - Rudolf Real

8

onCreateDialog中构建完AlertDialog后,您可以使用以下代码在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);

}

尝试在onCreateDialog方法中将可绘制对象添加到按钮中似乎不起作用。

会尝试并回复您,Frank。 - Ragunath Jawahar

6
这可以通过使用getButton()方法获取按钮的引用来完成:
alert.show();
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
email.setBackgroundResource(R.drawable.email);

请注意,在调用show()方法后必须使用getButton(),否则会出现NullPointerException异常。

我会检查并回复您。谢谢。 - Ragunath Jawahar

5
您不能在onCreateDialog中添加按钮,必须在onPrepareDialog中添加,因为AlertDialog在Android中以一种非常特殊的方式处理:
实际上,当您使用AlertDialog时,您并没有真正持有对真实对话框的引用,通过使用AlertDialog.Builder.create()获得的对象只是内部控制器的一个表面。
在create实际调用之前,jvm中没有这样的控制器。只有外观。因此,在调用此方法之前(如果您让活动管理自己的对话框,则在onCreateDialog的末尾),真正的控制器不存在,真正的按钮也不存在。
全新的SOF评论者,Stéphane

2

正如@aaronvargas所说,使用onShowListener。我会稍微改进他的答案,因为对于老旧/较小的设备,图像会重叠在文本上方。这是onShow代码:

@Override
public void onShow(DialogInterface dialogInterface) {
    Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_img, 0, 0, 0);
    Utils.centerImageAndTextInButton(button);
}

下面是一个实用的函数,可以将左侧图像和文本居中放置在一个 Button 中:

public static void centerImageAndTextInButton(Button button) {
    Rect textBounds = new Rect();
    //Get text bounds
    CharSequence text = button.getText();
    if (text != null && text.length() > 0) {
        TextPaint textPaint = button.getPaint();
        textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds);
    }
    //Set left drawable bounds
    Drawable leftDrawable = button.getCompoundDrawables()[0];
    if (leftDrawable != null) {
        Rect leftBounds = leftDrawable.copyBounds();
        int width = button.getWidth() - (button.getPaddingLeft() + button.getPaddingRight());
        int leftOffset = (width - (textBounds.width() + leftBounds.width()) - button.getCompoundDrawablePadding()) / 2 - button.getCompoundDrawablePadding();
        leftBounds.offset(leftOffset, 0);
        leftDrawable.setBounds(leftBounds);
    }
}

这个最后的函数使用 Button 的宽度进行计算,因此您必须检查您在正确的位置调用它。也就是说,宽度应该不为零。在这种情况下,从onShow调用它正确的位置 :)。


1

1.首先创建一个新的布局文件来存储图像按钮:new_layout.xml;

<?xml version="1.0" encoding="UTF-8" ?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_margin="15dp"
    android:gravity="center_horizontal"
    android:background = "#FFFFFF"
    android:orientation="horizontal">

    <!-- game button -->
    <ImageButton 
        android:id="@+id/game"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="5dp"
        android:layout_gravity="bottom"
        android:background = "#00ffffff"
        android:src="@drawable/game"/>

    <!-- browser button -->
    <ImageButton 
        android:id="@+id/browser"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="5dp"
        android:layout_gravity="bottom"
        android:background = "#00ffffff"
        android:src="@drawable/browser"/>

   <!-- email button -->
    <ImageButton 
        android:id="@+id/email"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_margin="5dp"
        android:layout_gravity="bottom"
        android:background = "#00ffffff"
        android:src="@drawable/email"/>

</LinearLayout>

2. 将下面的代码添加到您希望显示对话框的位置:

    final AlertDialog alertDialog = new AlertDialog.Builder(TalkerActivity.this).create();
    alertDialog.show();
    Window win = alertDialog.getWindow();
    win.setContentView(R.layout.new_layout);

    //Game
    ImageButton game_btn = (ImageButton)win.findViewById(R.id.game);
    game_btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    //Browser
    ImageButton browser_btn = (ImageButton)win.findViewById(R.id.browser);
    browser_btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    //Email
    ImageButton email_btn = (ImageButton)win.findViewById(R.id.email);
    email_btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

link:http://blog.csdn.net/willproud/article/details/9191971


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接