我在应用程序的警告对话框,如下图所示。
我想标题和标题分隔行 - 消息体是橙色。 我怎样才能做到这一点? 我尝试使用自定义样式,如下图所示的。 但这并没有工作。
<style name="AboutDialog" parent="@android:style/Theme.Dialog">
<item name="android:textColor">#E5492A</item>
</style>
我警告对话框代码:
AlertDialog.Builder alertDialog = new AlertDialog.Builder( new ContextThemeWrapper(MainActivity.context, R.style.AboutDialog));
alertDialog.setTitle("Sample");
alertDialog.setMessage(R.string.rate_dialog_text);
alertDialog.setPositiveButton(R.string.rate_now_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MainActivity.context.startActivity(new Intent(
Intent.ACTION_VIEW, Uri
.parse("market://details?id="
+ MainActivity.APP_PNAME)));
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
alertDialog.setNeutralButton(R.string.remind_later_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.setNegativeButton(R.string.no_thanks_text,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (editor != null) {
editor.putBoolean("dontshowagain", true);
editor.commit();
}
dialog.dismiss();
}
});
return alertDialog.create();
}
代替:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
new ContextThemeWrapper(MainActivity.context, R.style.AboutDialog));
试试这个:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, R.style.AboutDialog);
注意:这是仅适用于API 11(Android 3.0的)及以上。
如果你需要支持Android <3.0,你可能要创建一个自定义对话框(而不是使用AlertDialog
编辑新增真的单薄的基于反射的黑客
如果你真的卡住,不希望实现一个自定义对话框,然后尝试以下。
当你已经建立了对话,只是你想要的,而不是这回它,之前:
return alertDialog.create();
做这个:
AlertDialog ad = alertDialog.create(); // Create the dialog
// Add listener so we can modify the dialog before it is shown
ad.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
// Set the text color on the dialog title and separator
setTextColor(dialogInterface, 0xFFE5492A);
}
});
return ad;
现在加入这个非常讨厌的基于反射的方法:
public void setTextColor(DialogInterface alert, int color) {
try {
Class c = alert.getClass();
Field mAlert = c.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object alertController = mAlert.get(alert);
c = alertController.getClass();
Field mTitleView = c.getDeclaredField("mTitleView");
mTitleView.setAccessible(true);
Object dialogTitle = mTitleView.get(alertController);
TextView dialogTitleView = (TextView)dialogTitle;
// Set text color on the title
dialogTitleView.setTextColor(color);
// To find the horizontal divider, first
// get container around the Title
ViewGroup parent = (ViewGroup)dialogTitleView.getParent();
// Then get the container around that container
parent = (ViewGroup)parent.getParent();
for (int i = 0; i < parent.getChildCount(); i++) {
View v = parent.getChildAt(i);
if (v instanceof ImageView) {
// We got an ImageView, that should be the separator
ImageView im = (ImageView)v;
// Set a color filter on the image
im.setColorFilter(color);
}
}
} catch (Exception e) {
// Ignore any exceptions, either it works or it doesn't
}
}
我测试了在Android 2.2和Android 4.0和它的作品。 它可能不会做的正是你想要的东西,所以你需要去尝试。 我不能保证它会在所有设备上或在Android未来版本的工作,因为它取决于很多的方式AlertDialog
类实现(如果他们改变,在未来这可能会不再工作)。
只是任何人谁在乎几个注意事项:
我使用的原因setOnShowListener()
是因为你不能真正得到内部View
的对象AlertDialog
使用,直到他们被夸大了。 他们没有得到立即充气,当你创建一个AlertDialog
,后来发生了一些时间。 使用监听器我们可以得到控制的布局充气后,但在之前Dialog
显示。
我使用反射在内部成员变量访问AlertDialog
实施。 一旦我得到访问TextView
包含标题,我有猴子四处打听用来标题从消息中分离出来的水平线。 要做到这一点,我得到的Layout
,围绕标题(此Layout
包含警告图标和标题文本)。 然后我得到的Layout
,围绕该(这个Layout
包的图标,标题文本和分隔符)。 然后我看着周围的布局,所有儿童和设置颜色上所有ImageView
在那里的对象。 分离器被实现为一个ImageView
使用被拉伸,因此不能够只改变颜色。 注意:要使用替代setColorFilter()
将在这里适当的彩色绘制以替代的ImageView的绘制。
感谢您的挑战,这是一种乐趣啦!算出这个:-D
顺便说一下它的一晚的答案,也许这将是有用的人。 创建自定义AlertDialog和有像默认AlertDialog一样整齐的按钮。 按照下面的简单方法
final AlertDialog.Builder demoDialog = new AlertDialog.Builder(this);
final LayoutInflater inflator = this.getLayoutInflater();
final View view = inflator.inflate(R.layout.passcode_req_dialog_template, null);
demoDialog.setView(view)
.setPositiveButton(R.string.ok_button_text, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.cancel_button_text, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
final AlertDialog dialog = passwordDialog.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Get the work done here when pressing positive button
dialog.dismiss();
}
});
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Get the work done here when pressing negative button
dialog.dismiss();
}
});
尝试创建一个custom layout
的对话,并提供layout
,通过使用你的警告对话框
dialog.setContentView(R.layout.customDialogLayout);
你可以看到这个例子的自定义对话框。
你可以使用这个项目AlertDialogPro 。 包括这个项目你和定义主题象下面这样:
<style name="YourAppTheme.AlertDialogProTheme" parent="AlertDialogProTheme.Holo.Light">
<!-- Custom the title -->
<item name="android:windowTitleStyle">@style/YourDialogWindowTitle</item>
<!-- Change the title line to orange -->
<item name="adpTitleDividerBackground">#E5492A</item>
</style>
<style name="YourDialogWindowTitle" parent="DialogWindowTitle.Holo.Light">
<item name="android:textColor">#E5492A</item>
</style>
并指定这个主题,你的应用程序与属性“alertDialogProTheme”的主题:
<style name="AppTheme" parent="AppBaseTheme">
...
<item name="alertDialogProTheme">@style/YourAppTheme.AlertDialogProTheme</item>
</style>
使用AlertDialogPro.Builder建立对话:
AlertDialogPro.Builder builder = new AlertDialogPro.Builder(getContext());
builder.setTitle(R.string.app_name).
setMessage("Message").
setPositiveButton("Rate Now", null).
setNeutralButton("Remind me later", null).
setNegativeButton("No,thanks", null).
show();