Android AlertDialog按钮的图像
是否可以在AlertDialog的正、负和中性按钮中添加可绘图项?如果是,那么如何操作?在Android AlertDialog按钮的图像,android,user-interface,controls,Android,User Interface,Controls,是否可以在AlertDialog的正、负和中性按钮中添加可绘图项?如果是,那么如何操作?在onCreateDialog中构建AlertDialog后,可以使用onPrepareDialog中的以下代码将图像添加到正按钮: @Override protected void onPrepareDialog(int id, Dialog dialog) { super.onPrepareDialog(id, dialog); AlertDialog alertDialog = (Ale
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);
}
尝试将drawable添加到onCreateDialog方法中的按钮似乎不起作用。您无法在onCreateDialog中添加按钮,必须在onPrepareDialog中添加按钮,因为android以非常特殊的方式处理AlertDialog: 当您使用alert dialog时,实际上并没有真正保留对真实对话框的引用,使用AlertDialog.Builder.create()获得的对象只是内部控制器的一个面 在实际调用create之前,jvm中没有这样的控制器。只是门面。因此,在调用此方法之前(如果让您的活动管理自己的对话框,则在onCreateDialog的末尾),真正的控制器不存在,真正的按钮也不存在
全新的SOF commenter,Stéphane这可以通过使用getButton()方法获取对按钮的引用来实现:
请注意,在调用show()方法后必须使用getButton(),否则将得到NullPointerException..1。首先创建一个新的布局文件来存储imagebuttons: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>
链接:由于不推荐使用
onPrepareDialog
,因此可以使用onShowListener
此外,您还应设置可绘制边界,否则将放置在最左侧
下面的代码输出
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;
}
}
正如@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);
}
下面是一个实用功能,用于将左图像和文本置于按钮的中心位置:
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);
}
}
最后一个函数使用按钮的宽度进行计算,因此必须检查调用的位置是否正确。也就是说,宽度应该不是零。在这种情况下,从onShow
调用它是正确的位置:)。将尝试返回此Frank。将检查并返回给您。谢谢。你好,我想在播放文本时显示图标。那么你能给我提个建议吗?你能举个例子说明如何使用你的代码吗?如何实例化对话框?
@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);
}
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);
}
}