Có thể thêm drawables đến dương, tiêu cực và trung lập nút của một AlertDialog? Nếu có, thì làm thế nào?hình ảnh cho AlertDialog nút
Trả lời
Kể từ onPrepareDialog
bị phản đối, bạn có thể sử dụng onShowListener
để thay thế.
Ngoài ra, bạn nên đặt các giới hạn có thể vẽ hoặc nó sẽ được đặt ở bên trái.
Output của Bộ luật dưới đây
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;
}
}
Sau khi bạn đã xây dựng được các AlertDialog
trong onCreateDialog
bạn có thể sử dụng đoạn mã sau vào onPrepareDialog
để thêm hình ảnh vào nút tích cực:
@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);
}
Đang cố gắng để thêm drawable nút trong phương pháp onCreateDialog
không dường như hoạt động.
Sẽ cố gắng và quay lại với Frank này. –
Bạn không thể thêm các nút trong onCreateDialog và PHẢI làm điều đó trong onPrepareDialog vì AlertDialog được xử lý theo một cách rất đặc biệt bởi android:
bạn thực sự không thực sự giữ một tham chiếu đến hộp thoại thực sự khi bạn sử dụng hộp thoại cảnh báo, đối tượng bạn nhận được bằng cách sử dụng AlertDialog.Builder.create() chỉ là một khuôn mặt cho một bộ điều khiển bên trong.
Và trước khi tạo thực sự được gọi, không có bộ điều khiển như vậy trong jvm. Chỉ là mặt tiền. Vì vậy, cho đến khi phương thức này được gọi (vào cuối onCreateDialog nếu bạn cho phép hoạt động của bạn quản lý các hộp thoại riêng của nó), thì bộ điều khiển thực sự không tồn tại và các nút thực không có.
thương hiệu SOF mới commenter, Stéphane
Điều này có thể được thực hiện bằng cách nhận một tham chiếu đến các nút sử dụng) phương pháp getButton (:
alert.show();
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
email.setBackgroundResource(R.drawable.email);
Lưu ý rằng bạn phải sử dụng getButton() sau khi gọi phương thức show(), nếu không bạn sẽ nhận được một NullPointerException ..
Sẽ kiểm tra và liên hệ lại với bạn. Cảm ơn. –
1. đầu tiên tạo một tệp sơ đồ bố trí mới để lưu trữ các 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.Add mã dưới đây để nơi bạn muốn đối thoại để hiển thị:
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
}
});
liên kết: http://blog.csdn.net/willproud/article/details/9191971
Như @aaronvargas nói, sử dụng onShowListener
. Tôi sẽ cải thiện câu trả lời của mình một chút, vì đối với các thiết bị cũ hơn/nhỏ hơn, hình ảnh chồng chéo lên văn bản.Đây là mã 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);
}
Dưới đây là một chức năng tiện ích để tập trung một hình ảnh trái và văn bản bên trong một 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);
}
}
chức năng cuối cùng này sử dụng độ rộng của Button
để làm cho việc tính toán, vì vậy bạn phải kiểm tra xem bạn có đang gọi điều này ở đúng nơi không. Tức là, chiều rộng phải khác 0. Trong trường hợp này, hãy gọi điện thoại từ số onShow
là đúng địa điểm :).
- 1. hình ảnh cho nút Cancel của UISearchBar
- 2. Các nút kiểu AlertDialog cho một Hoạt động
- 3. Thay đổi màu nút trong AlertDialog
- 4. bộ hình ảnh như nút
- 5. Nút Hình ảnh trong BlackBerry
- 6. nút hình ảnh với html5
- 7. Làm thế nào để làm cho nút hình ảnh
- 8. Làm cho một hình ảnh hoạt động như một nút
- 9. iOS: Thanh điều hướng có hình ảnh cho các nút
- 10. Làm cho nút Hình ảnh được nhấn/bấm trong Swing
- 11. Tùy chỉnh listview cho AlertDialog
- 12. Highcharts: Thêm nút hình ảnh tùy chỉnh
- 13. Qt: tạo "nút hình ảnh svg"
- 14. Làm cách nào tôi có thể kéo hình ảnh trong nút hình ảnh thành toàn bộ vùng của nút ảnh?
- 15. Java: Hình ảnh làm nút chuyển đổi
- 16. Hình ảnh tùy chỉnh nút hủy UISearchbar
- 17. Tải hình ảnh cho hình ảnh tĩnh
- 18. Sự khác biệt giữa nút hình ảnh và nút
- 19. 'Graying Out' một hình ảnh nút WPF?
- 20. Phương pháp hình ảnh nút cài đặt
- 21. Cách tạo nút hình ảnh trong Android?
- 22. cách mã nút Hình ảnh trong PyQt?
- 23. hình ảnh nút thay đổi trong android
- 24. Hình ảnh iOS quay trên nút bấm
- 25. Hình thu nhỏ lớp phủ có nút xóa hình ảnh?
- 26. Chủ đề ánh sáng cho AlertDialog?
- 27. Nút hình ảnh hiển thị 'Gửi truy vấn' phía trên cùng của hình ảnh trong Firefox
- 28. thiết kế bố trí như bên dưới hình ảnh với các nút hình ảnh trong android
- 29. ListView trong AlertDialog
- 30. Android - Màu nền AlertDialog tùy chỉnh
Cảm ơn bạn đã cập nhật :) –
Xin chào Tôi muốn hiển thị biểu tượng gần hơn để phát văn bản. vậy bạn có thể gợi ý cho tôi không? –
Bạn có thể cho ví dụ về cách sử dụng mã của bạn không? Làm thế nào để khởi tạo hộp thoại? –