Android:如何在不使用自定义布局的情况下更改AlertDialog标题文本颜色和背景颜色?
我想在不使用自定义布局的情况下更改Android:如何在不使用自定义布局的情况下更改AlertDialog标题文本颜色和背景颜色?,android,android-alertdialog,background-color,textcolor,Android,Android Alertdialog,Background Color,Textcolor,我想在不使用自定义布局的情况下更改AlertDialog标题颜色和背景颜色。我的要求, 我尝试了下面的代码,但无法工作 final CharSequence[] items = {" Visiting Card", "Prescription Letter"}; AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setMessage(message)
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 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>
@颜色/颜色重音
#ffffff
@颜色/白色
@颜色/原色
您可以在警报对话框中使用自定义标题:
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();
// 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));
您也可以通过代码进行更改
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));
希望这对您有所帮助。我知道您不想这样做,但使用视图是最好的方法。 使用自定义视图,您可以非常轻松地更改每种颜色
LayoutInflater LayoutInflater=getLayoutInflater代码>
View view1=layoutinflater.flate(R.layout.yourlayout,null)代码>
view1
设置为生成器:builder.setView(view1)代码>
示例:
Textview tx=(Textview)view1.findViewById(R.id.Textview)
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>"));
alert.setTitle(Html.fromHtml(“Hello World”);
您可以更改警报对话框中的所有内容:
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();
// 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));
您可以设置主题:
newalertdialog.Builder(这是AlertDialog.THEME\u DEVICE\u DEFAULT\u DARK)代码>
或者您可以添加setonswowlistener()
如下所示:
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();
您可以使用自定义主题更改警报对话框标题、背景和按钮颜色
<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>
这对我来说很有效,很简单:
由于某种原因,我的对话框显示的标题背景与对话框背景不同,因此添加此代码后,问题就解决了
对话框显示后,使用您的颜色设置setBackgroundDrawable:
mDialog.show();
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(put here your color));
您可以通过try this的可能副本进行更改,但它对标题使用自定义视图。但是非常简单整洁的答案。@SaurabHandari,我的问题不同,我想更改标题文本背景而不是对话框背景。@SagarZala在主题中添加文本颜色属性,对于背景添加背景属性我不想更改按钮颜色。我想更改标题文本和背景颜色。我只想更改标题文本颜色和背景颜色。这将更改标题文本颜色是的,但是标题背景颜色如何?现在这正是您想要的。我的问题是不使用自定义布局,但这是一个很好的解决方案。现在我可以用这个了。谢谢。不,我不想使用自定义布局。@SagarZala为什么?这很简单,而且您有更多选项可以使用样式自定义AlertDialogUsing我可以更改字体颜色,背景如何?为此,请尝试dialog.getWindow().setBackgroundDrawableResource(R.color.glassblack)代码>…omik,这会将整个对话框背景更改为该颜色。不是标题。没有有关AlertDialog
title的帮助。getResources()。getIdentifier(“alertTitle”、“id”、“android”)
不起作用。返回空对象。