AlertDialog按钮的图像

时间:2010-09-09 18:26:03

标签: android user-interface controls

是否可以将drawable添加到AlertDialog的正面否定中性按钮?如果是,那怎么办?

6 个答案:

答案 0 :(得分:14)

由于不推荐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;
    }
}

答案 1 :(得分: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);

}

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

答案 2 :(得分:5)

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

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

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

全新的SOF评论者,Stéphane

答案 3 :(得分:5)

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

alert.show();
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
email.setBackgroundResource(R.drawable.email);

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

答案 4 :(得分: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 调用它是正确的地方:)。

答案 5 :(得分: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

        }
    });

链接:http://blog.csdn.net/willproud/article/details/9191971