我在应用程序中有一个警告对话框,如下所示。
我希望标题和分隔标题的线 - 消息正文为橙色。我怎样才能做到这一点? 我尝试的是使用自定义样式,如下所示。但这没有用。
<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
EDIT 添加了非常低级的基于反射的 hack
如果您真的遇到困难并且不想实现自定义对话框,请尝试以下操作。
在你构建对话框之后,就在你想要返回它之前,而不是这样:
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 上对此进行了测试,它可以正常工作。它可能无法完全满足您的要求,因此您需要尝试一下。我不能保证它能在所有设备或 future 版本的 Android 上运行,因为它在很大程度上取决于 AlertDialog
类的实现方式(如果他们将来改变它,这可能不会'不再工作了)。
给关心的人一些注意事项:
我使用
setOnShowListener()
的原因是因为您实际上无法访问AlertDialog
的内部View
对象使用直到充气后。当您创建AlertDialog
时,它们不会立即膨胀,它会在一段时间后发生。使用监听器,我们可以在布局展开之后但在显示Dialog
之前获得控制权。我在
AlertDialog
实现中使用反射来访问内部成员变量。访问包含标题的TextView
后,我必须四处寻找用于将标题与消息分开的水平线。为此,我获取围绕标题的Layout
(此Layout
包含警告图标和标题文本)。然后我得到围绕它的Layout
(这个Layout
包裹着图标、标题文本和分隔符)。然后我查看周围布局的所有子项并为其中的所有ImageView
对象设置颜色。分隔符是使用可绘制对象作为ImageView
实现的,因此不可能仅更改颜色。注意:使用setColorFilter()
的替代方法是在此处用适当颜色的可绘制对象替换 ImageView 中的可绘制对象。
感谢您的挑战,解决这个问题很有趣:-D
关于Android - AlertDialog 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14770400/