您可以执行以下操作
builder.setMessage(Html.fromHtml("<font color='#FF0000'><b>John:</b></font>"+"How are you?"));
OR
<string name="Howtoplay"><![CDATA[html data here ]]>
</string>
builder.setMessage(Html.fromHtml(getString(R.string.HowtoPlay)));
OR
使用自定义对话框,然后您可以设置文本的颜色。
如何在android的警报对话框中的文本消息中应用颜色
编辑:
您可以按空格拆分单词,然后设置可扩展的字符串前景。还可以为对话框中的每个单词提供自定义颜色。
您还可以从http://www.colorhunter.com/tag/android/1指定颜色
主要的.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_marginBottom="84dp"
android:layout_marginRight="18dp"
android:text="Button" />
dialog.xml 自定义布局被膨胀
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="127dp"
android:text="CANCEL" />
</RelativeLayout>
你的 MainActivity
public class MainActivity extends Activity {
String[] each;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showpopup();
}
});
}
public void showpopup()
{
final Dialog d = new Dialog(MainActivity.this);
d.setTitle("my title");
d.setContentView(R.layout.dialog);
TextView tv= (TextView) d.findViewById(R.id.editText1);
String s="Apple Bannana Mango Orange";
tv.setText("");
for(int i=0;i<s.length();i++)
{
each = s.split("\\s+");
}
for(int i=0;i<each.length;i++)
{
SpannableString ss1= new SpannableString(each[i]);
if(each[i].equals("Apple"))
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, ss1.length(), 0);
else if(each[i].equals("Bannana"))
ss1.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, ss1.length(), 0);
else if(each[i].equals("Mango"))
ss1.setSpan(new ForegroundColorSpan(Color.GREEN), 0, ss1.length(), 0);
else if(each[i].equals("Orange"))
ss1.setSpan(new ForegroundColorSpan(Color.GRAY), 0, ss1.length(), 0);
tv.append(ss1);
tv.append(" ");
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
Button cancel = (Button) d.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
d.cancel();
}
});
d.show();
}
}
生成的快照