AlertDialog 不显示中性按钮图标
AlertDialog not displaying Neutral Button Icon
我是 android 开发方面的新手。
创建我的方法时,我会显示一个警告对话框,显示被调用 activity 的 select 选项。
但是,它不显示中性按钮图标,而是调用相关操作。
它会在点击时显示图像。请参考下面给出的代码和图片链接。
代码是这样的:
initDialogBuilder.setCancelable(false)
.setTitle("Select your counter")
.setPositiveButtonIcon(getDrawable(R.drawable.o))
.setPositiveButton("",listener)
.setNegativeButtonIcon(getDrawable(R.drawable.x))
.setNegativeButton("",listener)
.setNeutralButtonIcon(getDrawable(R.drawable.sq))
.setNeutralButton("",listener)
.setMessage("Please select your counter.");
AlertDialog initDialog = initDialogBuilder.create();
initDialog.show();
样本输出是这样的:Click here to see sample output with icons attached.
但是,在删除图标并添加标题时,它会显示文本。
另一个带有文本的代码是这样的:
initDialogBuilder.setCancelable(false)
.setTitle("Select your counter")
.setPositiveButton("X",listener)
.setNegativeButton("O",listener)
.setNeutralButton("SQ",listener)
.setMessage("Please select your counter.");
AlertDialog initDialog = initDialogBuilder.create();
initDialog.show();
此处显示的是带有文本而不是图标的输出。 Click here to see sample output with text.
我该怎么办?还有其他改进我的 UI 的建议吗?请帮忙。
请试试这个代码,它对我来说工作正常
Without netural button text icon not visible so i have added one space in netural button text and set button icon code after show dialog.check below code and screen shot
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
//Uncomment the below code to Set the message and title from the strings.xml file
builder.setMessage("Custom dialog with neutral button") .setTitle("Just R&D");
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",
Toast.LENGTH_SHORT).show();
}
}).setPositiveButtonIcon(getDrawable(R.drawable.ic_android_black_24dp))
.setNegativeButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
Toast.makeText(getApplicationContext(),"you choose no action for alertbox",
Toast.LENGTH_SHORT).show();
}
}).setNegativeButtonIcon(getDrawable(R.drawable.ic_android_black_24dp)).setNeutralButton(" ", new DialogInterface.OnClickListener() { //need to add neutral button text
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})/*.setNeutralButtonIcon(getDrawable(R.drawable.ic_android_black_24dp))*/;
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
Button button = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
Drawable drawable = this.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);
我是 android 开发方面的新手。 创建我的方法时,我会显示一个警告对话框,显示被调用 activity 的 select 选项。 但是,它不显示中性按钮图标,而是调用相关操作。 它会在点击时显示图像。请参考下面给出的代码和图片链接。 代码是这样的:
initDialogBuilder.setCancelable(false)
.setTitle("Select your counter")
.setPositiveButtonIcon(getDrawable(R.drawable.o))
.setPositiveButton("",listener)
.setNegativeButtonIcon(getDrawable(R.drawable.x))
.setNegativeButton("",listener)
.setNeutralButtonIcon(getDrawable(R.drawable.sq))
.setNeutralButton("",listener)
.setMessage("Please select your counter.");
AlertDialog initDialog = initDialogBuilder.create();
initDialog.show();
样本输出是这样的:Click here to see sample output with icons attached.
但是,在删除图标并添加标题时,它会显示文本。 另一个带有文本的代码是这样的:
initDialogBuilder.setCancelable(false)
.setTitle("Select your counter")
.setPositiveButton("X",listener)
.setNegativeButton("O",listener)
.setNeutralButton("SQ",listener)
.setMessage("Please select your counter.");
AlertDialog initDialog = initDialogBuilder.create();
initDialog.show();
此处显示的是带有文本而不是图标的输出。 Click here to see sample output with text.
我该怎么办?还有其他改进我的 UI 的建议吗?请帮忙。
请试试这个代码,它对我来说工作正常
Without netural button text icon not visible so i have added one space in netural button text and set button icon code after show dialog.check below code and screen shot
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(this);
//Uncomment the below code to Set the message and title from the strings.xml file
builder.setMessage("Custom dialog with neutral button") .setTitle("Just R&D");
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.makeText(getApplicationContext(),"you choose yes action for alertbox",
Toast.LENGTH_SHORT).show();
}
}).setPositiveButtonIcon(getDrawable(R.drawable.ic_android_black_24dp))
.setNegativeButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
Toast.makeText(getApplicationContext(),"you choose no action for alertbox",
Toast.LENGTH_SHORT).show();
}
}).setNegativeButtonIcon(getDrawable(R.drawable.ic_android_black_24dp)).setNeutralButton(" ", new DialogInterface.OnClickListener() { //need to add neutral button text
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})/*.setNeutralButtonIcon(getDrawable(R.drawable.ic_android_black_24dp))*/;
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
Button button = alert.getButton(AlertDialog.BUTTON_NEUTRAL);
Drawable drawable = this.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);