Android 是否更改警报对话框中的文本颜色?
我正在尝试创建一个警告对话框,询问用户一个问题,消息中的一些文本将显示为红色 这就是我尝试过的:Android 是否更改警报对话框中的文本颜色?,android,android-layout,Android,Android Layout,我正在尝试创建一个警告对话框,询问用户一个问题,消息中的一些文本将显示为红色 这就是我尝试过的: AlertDialog.Builder dlgAlert = new AlertDialog.Builder( this); String You = "<font color='red'>you</font>?"; dlgAlert.setMessage("How
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
this);
String You = "<font color='red'>you</font>?";
dlgAlert.setMessage("How are " + Html.fromHtml(You));
dlgAlert.setTitle("Mood");
dlgAlert.setPositiveButton("Good",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setNeutralButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setNegativeButton("Bad",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();
AlertDialog.Builder dlgAlert=新建AlertDialog.Builder(
这),;
String You=“You?”;
setMessage(“你好”+Html.fromHtml(你));
dlgAlert.setTitle(“心情”);
dlgAlert.setPositiveButton(“好”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(其中){
dialog.dismise();
}
});
dlgAlert.setNeutralButton(“好的”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(其中){
dialog.dismise();
}
});
dlgAlert.setNegativeButton(“坏”,
新建DialogInterface.OnClickListener(){
公共void onClick(对话框接口对话框,
int(其中){
dialog.dismise();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();
它不会把“你”这个词变成红色。有什么想法吗 如果没有自定义警报框,这是不可能的,如图所示:如果没有自定义警报框,这是不可能的,如图所示:这里有一种方法 在您的
活动中
:
LayoutInflater factory = LayoutInflater.from(this);
View dialog = factory.inflate(R.layout.example_dialog, null);
TextView title = (TextView) dialog.findViewById(R.id.message);
SpannableString text = new SpannableString("Test 123");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 1, 2, 0);
text.setSpan(new ForegroundColorSpan(Color.DKGRAY), 2, 3, 0);
text.setSpan(new ForegroundColorSpan(Color.CYAN), 3, 4, 0);
title.setText(text, BufferType.SPANNABLE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialog)
.setTitle("Title")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do something
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.create()
.show();
在/layouts/example_dialog.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#F70000" />
</LinearLayout>
标题也可以有一个自定义的标题视图,您可以使用
setCustomTitle(视图视图)
来设置它
在您的活动中
:
LayoutInflater factory = LayoutInflater.from(this);
View dialog = factory.inflate(R.layout.example_dialog, null);
TextView title = (TextView) dialog.findViewById(R.id.message);
SpannableString text = new SpannableString("Test 123");
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 1, 2, 0);
text.setSpan(new ForegroundColorSpan(Color.DKGRAY), 2, 3, 0);
text.setSpan(new ForegroundColorSpan(Color.CYAN), 3, 4, 0);
title.setText(text, BufferType.SPANNABLE);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialog)
.setTitle("Title")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do something
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.create()
.show();
在/layouts/example_dialog.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#F70000" />
</LinearLayout>
标题也可以具有标题的自定义视图,您可以使用
setCustomTitle(视图视图)
设置标题,这似乎是用于网页上的警告对话框。是的,我在评论后意识到了我的错误,但“删除”似乎只是要求投票删除评论,我不确定这是否是正确的做法。这似乎是针对网页上的警告对话框。是的,我在发表评论后意识到了我的错误,但“删除”似乎只是要求投票删除评论,我不确定这是否是正确的做法。