[英]Android Alert Dialog - How to change the title color and the text color without changing the primary color
首先,创建一个这样的样式定义:
<style name="DialogTheme" parent="ThemeOverlay.AppCompat.Dialog">
<item name="android:textColorPrimary">your color here</item>
</style>
然后,在创建对话框时,不使用AlertDialog.Builder(Context)
构造函数, AlertDialog.Builder(Context, int)
使用AlertDialog.Builder(Context, int)
方法并将引用传递给您的样式:
new AlertDialog.Builder(this, R.style.DialogTheme)
.setTitle("Hello world")
.setMessage("some longer text for the body of the dialog")
.show();
即使这取决于更改textColorPrimary
,但它不会以影响应用程序中任何其他内容的方式执行此操作。
尝试这个,
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(Html.fromHtml("<font color='#0288D1'>This is a test</font>"));
builder.setPositiveButton(Html.fromHtml("<font color='#0288D1'>Yes</font>"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Log.e(LOG_TAG, "Yes");
}
});
builder.setNegativeButton(Html.fromHtml("<font color='#0288D1'>No</font>"), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Log.e(LOG_TAG, "No");
}
});
builder.create();
builder.show();
简单易行:
SpannableString title = new SpannableString("Your title");
title.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.your_color)), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
然后只是作为标题传递
new AlertDialogBuilder(context).setTitle(title).show();
对消息做同样的事情。
简单地说,您只需使用以下代码更改“TITLE”文本:
Html.fromHtml("<font color='#FFFFFF'>TITLE</font>")
此代码将完全更改您的文本颜色,具体取决于您添加的颜色值。 您也可以将它用于按钮的文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.