是否有可能绘制资源添加到AlertDialog的积极,负和中性按钮?如果是,那么如何?图片为AlertDialog按钮
回答
由于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;
}
}
后你已经建立了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);
}
试图将绘制添加到按钮在onCreateDialog
方法不似乎工作。
会试着回到这个弗兰克。 – 2010-09-17 07:15:29
你不能在onCreateDialog添加按钮,必须做它在onPrepareDialog因为AlertDialog在一个非常特殊的方式,通过机器人处理:
你其实并不真正持有到真正的对话时参考你使用alert对话框,通过使用AlertDialog.Builder.create()获得的对象只是内部控制器的一个面。
而在创建之前实际上是调用的,在jvm中没有这样的控制器。只是门面。所以,直到这个方法被调用(在onCreateDialog结束时,如果你让你的活动管理自己的对话框),真正的控制器不存在,真正的按钮不会。以后打电话
alert.show();
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
email.setBackgroundResource(R.drawable.email);
请注意,您必须使用getButton():
全新的SOF评议,斯特凡
这可以通过获取使用getButton()方法的按钮的引用来完成show()方法,否则你会得到一个NullPointerException ..
将检查并回复给您。谢谢。 – 2012-03-31 12:36:42
1.first创建一个新的布局文件来存储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>
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
}
});
输出作为@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. AlertDialog单按钮?
- 2. Android AlertDialog单按钮
- 3. AlertDialog按钮打开新的AlertDialog
- 4. 的Android AlertDialog按钮定位
- 5. alertDialog按钮转到URI
- 6. 动态alertdialog单选按钮
- 7. alertdialog中按钮的大小
- 8. JavaFX的图片作为按钮
- 9. 图片显示为按钮空白
- 10. 使用图片作为提交按钮
- 11. 组图片作为提交按钮
- 12. 将图片设置为按钮
- 13. 将链接转为图片“按钮”
- 14. 点击图片作为按钮?
- 15. 按钮图片的不同行为
- 16. JSP - 图片按钮在点击后成为图片
- 17. 使用图片作为按钮并点击切换图片
- 18. 防止在AlertDialog上按多个按钮
- 19. AlertDialog总是按下正向按钮
- 20. iPhone - 按钮背景图片
- 21. 图片按钮ID冲突?
- 22. 拖放图片到按钮
- 23. 图片库变暗按钮
- 24. wxPython按钮背景图片
- 25. 图片上传按钮div
- 26. REWRITE:Navlinks图片按钮效果
- 27. iOS按钮backgroundImage vs图片
- 28. ckeditor上传图片按钮
- 29. wpf按钮背景图片
- 30. Android - 图片按钮大小
谢谢你的更新:) – 2013-12-30 07:18:04
你好,我想显示图标更接近播放文本。所以你可以请我建议吗? – 2014-09-12 05:54:37
你可以举一个如何使用你的代码的例子吗?如何实例化对话框? – 2014-11-29 21:28:34