是否有可能繪製資源添加到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