我想更改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));
}
使用我尝试过的以下行,但它总是在null
中返回findViewById
,
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>
答案 0 :(得分:1)
您可以在警报对话框中使用自定义标题:
TextView textView = new TextView(context);
textView.setText("Select an option");
textView.setPadding(20, 30, 20, 30);
textView.setTextSize(20F);
textView.setBackgroundColor(Color.CYAN);
textView.setTextColor(Color.WHITE);
final CharSequence[] items = {"Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCustomTitle(textView);
builder.setItems(items, (dialog, item) -> {
}).show();
答案 1 :(得分:1)
我知道您不希望这样做,但是使用View是最好的方法。 使用自定义视图,您可以非常轻松地更改每种颜色。
只需创建一个Layout并将其放置在其中(并更改颜色)
创建LayoutInflater:LayoutInflater layoutinflater = getLayoutInflater;
设置如下视图:View view1 = layoutinflater.inflate(R.layout.yourlayout, null);
为您的构建器设置view1
:builder.setView(view1);
示例:Textview tx = (TextView)view1.findViewById(R.id.textview)
答案 2 :(得分:1)
IMO,您在设置颜色之前先致电dialog.show();
,所以请尝试以下代码
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
AlertDialog dialog = builder.create();
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));
}
dialog.show(); //change here
更新
只需尝试设置标题以提醒行下方的位置
alert.setTitle( Html.fromHtml("<font color='#FF7F27'>Hello World</font>"));
答案 3 :(得分:1)
您可以使用自定义主题更改警报对话框标题的颜色,背景和按钮的颜色。
<style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@android:color/black</item>
<item name="colorAccent">@android:color/white</item>
<item name="android:textColorPrimary">@android:color/white</item>
</style>
android:windowBackground更改背景颜色
colorAccent更改按钮颜色
android:textColorPrimary更改对话框标题颜色
将此主题应用于对话框
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialogTheme);
答案 4 :(得分:0)
您可以在警报对话框中更改所有内容:
// Title
TextView titleView = new TextView(context);
titleView.setText("Title");
titleView.setGravity(Gravity.CENTER);
titleView.setPadding(20, 20, 20, 20);
titleView.setTextSize(20F);
titleView.setTypeface(Typeface.DEFAULT_BOLD);
titleView.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
titleView.setTextColor(ContextCompat.getColor(context, R.color.colorAccent));
AlertDialog ad = new AlertDialog.Builder(context).create();
ad.setCustomTitle(titleView);
ad.setCancelable(false);
ad.setMessage("Message");
ad.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// your code
}
});
ad.show();
// Message
TextView messageView = ad.findViewById(android.R.id.message);
if (messageView != null) {
messageView.setGravity(Gravity.CENTER);
}
// Buttons
Button buttonOK = ad.getButton(DialogInterface.BUTTON_POSITIVE);
buttonOK.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
答案 5 :(得分:0)
您可以设置主题:
new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_DARK);
或者您可以如下添加setOnShowListener()
:
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.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
int titleId = getResources().getIdentifier("alertTitle", "id", "android");
TextView dialogTitle = (TextView) dialog.findViewById(titleId);
dialogTitle.setTextColor(Color.WHITE);
dialogTitle.setBackgroundColor(Color.BLACK);
}
});
dialog.show();
答案 6 :(得分:0)
这对我有用,很简单: 我的对话框出于某种原因显示的标题背景不同于对话框背景,因此添加此代码后,问题得以解决。
对话框显示后,用您的颜色设置setBackgroundDrawable:
mDialog.show();
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(put here your color));
答案 7 :(得分:-1)
您也可以通过代码进行更改。
AlertDialog dialog = builder.create();
dialog.show();
Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
buttonPositive.setTextColor(ContextCompat.getColor(this, R.color.green));
Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonNegative.setTextColor(ContextCompat.getColor(this, R.color.red));
希望对您有帮助。