【发布时间】:2018-12-25 01:57:41
【问题描述】:
我想在不使用自定义布局的情况下更改 AlertDialog 标题颜色 和 背景颜色。我的要求,
我尝试了以下代码,但无法正常工作。
final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
builder.setItems(items, (dialog, item) -> {
});
AlertDialog dialog = builder.create();
dialog.show();
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
tv.setTextColor(activity.getResources().getColor(R.color.white));
tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}
我尝试使用以下行,但它总是在findViewById 中返回null,
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId);
我也尝试过使用style,但它只会改变标题文本的颜色,
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#ffffff</item>
<item name="android:textColor">@color/white</item>
<item name="android:headerBackground">@color/colorPrimary</item>
</style>
【问题讨论】:
-
您可以通过主题更改
-
试试这个,但它使用自定义视图作为标题。但非常简单整洁的ans。 stackoverflow.com/a/12813088/6060743
-
@SaurabhBhandari,我的问题不同,我想更改标题文本背景而不是对话框背景。
-
@SagarZala 在主题中添加您的文本颜色属性并为背景添加背景属性
标签: android android-alertdialog background-color textcolor