Android 使用 Html.fromHtml 在 TextView 中设置文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12566371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:02:14  来源:igfitidea点击:

Set text in TextView using Html.fromHtml

androidandroid-layouttextview

提问by zaiff

I have to show a text in three parts, so i have used Html.fromHtml like this:

我必须分三部分显示文本,所以我使用了 Html.fromHtml 像这样:

txtvw.setText(Html.fromHtml("<p align=right> <b> "
                    + "Hi!" + " </br> <font size=6>"
                    + " How are you "+"</font> </br>"
                    + "I am fine" + "  </b> </p>"));

The HTML is correct but in device it's showing in one line.

HTML 是正确的,但在设备中它显示在一行中。

my xml declaration of textview is:

我的 textview xml 声明是:

   <RelativeLayout
    android:id="@+id/Home"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:background="@drawable/transparentfooter"
    android:layout_above="@+id/bottombar" >


     <TextView 
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="@android:color/white"/>

    </RelativeLayout>

回答by Arun George

The way you used the <br>tag is inappropriate. Use the following:

您使用<br>标签的方式不合适。使用以下内容:

txtvw.setText(Html.fromHtml("<p align=right> <b> "
            + "Hi!" + " <br/> <font size=6>"
            + " How are you "+"</font> <br/>"
            + "I am fine" + "  </b> </p>"));

It should be <br/>and not </br>I have tested this code and it displays the 3 lines as expected.

它应该是,<br/>而不是</br>我已经测试过这段代码,它按预期显示了 3 行。

回答by Lins Louis

Html.fromHtml(String source)

now Deprecated from Api-24.

现在已从 Api-24 弃用。

From Api-24 this method changed to

从 Api-24 开始,此方法更改为

Html.fromHtml(String source,int flags)

So we can use like below from Api 24

所以我们可以在 Api 24 中使用如下所示

txtvw.setText(Html.fromHtml("<p align=right> <b> "
            + "Hi!" + " <br/> <font size=6>"
            + " How are you "+"</font> <br/>"
            + "I am fine" + "  </b> </p>"),Html.FROM_HTML_MODE_LEGACY);

回答by Yahor10

Set lines tag to your layout android:lines="4"

将行标记设置为您的布局 android:lines="4"

<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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:lines="4"                
        android:text="@string/hello_world"
        tools:context=".MainActivity" />

</RelativeLayout>

Write correct "br" html tags

编写正确的“br”html标签

 TextView text =(TextView)findViewById(R.id.text);        
        text .setText(Html.fromHtml("<p align=right> <b> "
                + "<br>" +"Hi!" + "  </br> "
                + "<br> How are you "+" </br>"
                + "<br>I am fine" + " </br> </b> </p>"));

回答by developerSumit

textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));

textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));