Java 警报对话框未显示strings.xml中定义的HTML

Java 警报对话框未显示strings.xml中定义的HTML,java,android,eclipse,textview,android-alertdialog,Java,Android,Eclipse,Textview,Android Alertdialog,请看一下下面的代码 strings.xml <string name="measurements_description"><html><font color="#FFFAF0">I want to have white color for the whole text. <br/>This text comes in a new line<font color="red"

请看一下下面的代码

strings.xml

<string name="measurements_description"><html><font color="#FFFAF0">I want to have white color for the whole text. 
    <br/>This text comes in a new line<font color="red">This text is red </font>
    <br/>This is another line. Again, color is white </font></html></string>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView 
            android:id="@+id/measurementsDescription"
            android:text="@string/measurements_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>
</ScrollView>
...
<TextView 
    android:id="@+id/measurementsDescription"

    android:textColor="#FFFFFF"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
...
<string name="measurements_description">
<![CDATA[
    I want to have white color for the whole text. 
    <br/>This text comes in a new line <font color="red">This text is red </font>
    <br/>This is another line. Again, color is white
]]>    
</string>
我希望整个文本都是白色。

此文本以新行显示此文本为红色
这是另一行。同样,颜色是白色的
male_layout.xml

<string name="measurements_description"><html><font color="#FFFAF0">I want to have white color for the whole text. 
    <br/>This text comes in a new line<font color="red">This text is red </font>
    <br/>This is another line. Again, color is white </font></html></string>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView 
            android:id="@+id/measurementsDescription"
            android:text="@string/measurements_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>
</ScrollView>
...
<TextView 
    android:id="@+id/measurementsDescription"

    android:textColor="#FFFFFF"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
...
<string name="measurements_description">
<![CDATA[
    I want to have white color for the whole text. 
    <br/>This text comes in a new line <font color="red">This text is red </font>
    <br/>This is another line. Again, color is white
]]>    
</string>

Form.java(这属于主布局活动\u表单)

public boolean onMenuItemSelected(int-id,MenuItem-item){
//Toast.makeText(this,item.getTitle(),Toast.LENGTH_LONG.show();
TextView msg=新的TextView(此);
msg.setText(“消息”);
AlertDialog.Builder alertBuilder=新建AlertDialog.Builder(此);
//alertBuilder.setMessage(“消息”);
alertBuilder.setTitle(“男性测量说明”);
LayoutInflater li=getLayoutInflater();
视图v=li.充气(R.布局.外螺纹布局,空);
alertBuilder.setView(v);
alertBuilder.setCancelable(假);
alertBuilder.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口arg0,int arg1){
//alertBuilder.finish();
}
});
AlertDialog AlertDialog=alertBuilder.create();
alertDialog.show();
返回true;
}
这就是它的显示方式,不响应string.xml文件中的任何HTML。为什么这不改变HTML编码的颜色?为什么它不保留HTML中的换行符


如何解决这个问题?请帮忙

第一个问题是嵌套的
。如果您在另一个
内有
,则它将不起作用。您可以将
android:textColor
属性设置为
#FFFFFF
,然后使用
可以更改某些文本的颜色。第二,您必须在字符串中使用
CDATA
,第三,您必须从代码中设置
TextView
的文本,以便使用html格式化字符串。所以你一定有

male_layout.xml

<string name="measurements_description"><html><font color="#FFFAF0">I want to have white color for the whole text. 
    <br/>This text comes in a new line<font color="red">This text is red </font>
    <br/>This is another line. Again, color is white </font></html></string>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView 
            android:id="@+id/measurementsDescription"
            android:text="@string/measurements_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>
</ScrollView>
...
<TextView 
    android:id="@+id/measurementsDescription"

    android:textColor="#FFFFFF"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
...
<string name="measurements_description">
<![CDATA[
    I want to have white color for the whole text. 
    <br/>This text comes in a new line <font color="red">This text is red </font>
    <br/>This is another line. Again, color is white
]]>    
</string>

第一个问题是嵌套的
。如果您在另一个
内有
,则它将不起作用。您可以将
android:textColor
属性设置为
#FFFFFF
,然后使用
可以更改某些文本的颜色。第二,您必须在字符串中使用
CDATA
,第三,您必须从代码中设置
TextView
的文本,以便使用html格式化字符串。所以你一定有

male_layout.xml

<string name="measurements_description"><html><font color="#FFFAF0">I want to have white color for the whole text. 
    <br/>This text comes in a new line<font color="red">This text is red </font>
    <br/>This is another line. Again, color is white </font></html></string>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView 
            android:id="@+id/measurementsDescription"
            android:text="@string/measurements_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>
</ScrollView>
...
<TextView 
    android:id="@+id/measurementsDescription"

    android:textColor="#FFFFFF"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
...
<string name="measurements_description">
<![CDATA[
    I want to have white color for the whole text. 
    <br/>This text comes in a new line <font color="red">This text is red </font>
    <br/>This is another line. Again, color is white
]]>    
</string>
说明
文本视图
只支持少数HTML标记(包括

如果你想显示不同颜色的单词,你应该使用

一个完整的例子

希望能有帮助

编辑:

其他方法:

String text = "<font color=#cc0029>Erste Farbe</font> <font color=#ffcc00>zweite Farbe</font>";
yourtextview.setText(Html.fromHtml(text));
String text=“Erste Farbe zweite Farbe”;
yourtextview.setText(Html.fromHtml(text));
表示
文本视图
只支持少数HTML标记(包括

如果你想显示不同颜色的单词,你应该使用

一个完整的例子

希望能有帮助

编辑:

其他方法:

String text = "<font color=#cc0029>Erste Farbe</font> <font color=#ffcc00>zweite Farbe</font>";
yourtextview.setText(Html.fromHtml(text));
String text=“Erste Farbe zweite Farbe”;
yourtextview.setText(Html.fromHtml(text));

您可以通过在strings.xml中声明字符串来使用HTML格式+格式字符串

<string name="key">
    <![CDATA[
    My text and tags <b>with or without</b> variables<br/><br/>
    Answer is <u>%1$s</u>
    ]]>
</string>

通过在strings.xml中声明字符串,可以使用HTML格式+格式字符串

<string name="key">
    <![CDATA[
    My text and tags <b>with or without</b> variables<br/><br/>
    Answer is <u>%1$s</u>
    ]]>
</string>

你的意思是直接在对话框中显示
和其他标记?@Glenn:我的意思是,他们没有做他们应该做的事情。如转换颜色、添加换行符等。不支持HTML标记

,但您可以使用
\n
。对于着色,您可以设置文本视图的字体颜色,如
color.BLACK
或解析颜色,如
color.parseColor(“#FFFAF0”)
。您也可以尝试
WebView
确定,因此在这种情况下,我必须在te layout.xml文件中编辑它。但是,在文本中有一个地方,只有几个单词的颜色变为“红色”。请帮助你的意思是直接在对话框中显示
和其他标记?@Glenn:我的意思是,他们没有做他们应该做的事情。如转换颜色、添加换行符等。不支持HTML标记

,但您可以使用
\n
。对于着色,您可以设置文本视图的字体颜色,如
color.BLACK
或解析颜色,如
color.parseColor(“#FFFAF0”)
。您也可以尝试
WebView
确定,因此在这种情况下,我必须在te layout.xml文件中编辑它。但是,在文本中有一个地方,只有几个单词的颜色变为“红色”。请帮忙,我试过了。在Form.java中设置它不起作用。一个错误是在我回答之前让generatedI实现它,所以我知道它是有效的。发布错误,以便我可以帮助您。当然还有一些代码。对你的问题进行编辑我试过了。在Form.java中设置它不起作用。一个错误是在我回答之前让generatedI实现它,所以我知道它是有效的。发布错误,以便我可以帮助您。当然还有一些代码。对你的问题进行编辑