Java android中多行粗体标题的文本

Java android中多行粗体标题的文本,java,android,android-webview,Java,Android,Android Webview,我想写两页的文字,其中包括多个段落,一些要点和一些联系电话。 请帮助我一些参考教程或代码。我试过使用textview,但没有那么有效。请为我提供有效的解决方案 我的xml文件: <TextView android:id="@+id/simpleTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="All text here" android:textSi

我想写两页的文字,其中包括多个段落,一些要点和一些联系电话。 请帮助我一些参考教程或代码。我试过使用textview,但没有那么有效。请为我提供有效的解决方案

我的xml文件:

<TextView
android:id="@+id/simpleTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="All text here"
android:textSize="20sp"
android:gravity="center_horizontal"/>

将HTML文本保存到strings.xml文件中,然后将它们访问到类中,并按如下所示从HTML设置文本

 textView.setText(Html.fromHtml(getString(R.string.app_name)));

您可以使用
fromHtml
将文本设置为HTML

使用以下代码获取结果文本:

public static Spanned fromHtml(String html) {
        Spanned result;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            result = Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
        } else {
            result = Html.fromHtml(html);
        }
        return result;
    }
在代码中使用它,如:

您的_text_view.setText(fromHtml(getResources().getString(R.string.htmlText))


您可以将文本格式化为html,并使用以下函数将html(具有该格式)呈现给您的textview

yourTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
yourTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
yourTextView.setText(Html.fromHtml(“Title
此处的说明

”);
>=安卓牛轧糖:

yourTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
yourTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
yourTextView.setText(Html.fromHtml(“Title
Description here

”,Html.FROM\u Html\u MODE\u COMPACT));
要区分Android版本,请使用


Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.N.

这是html代码的示例

<!DOCTYPE html>
<html>
<body>

<h2>Unordered List with Square Bullets</h2>

<ul style="list-style-type:square">
  <li>Coffee</li>
  <li>Tea</li>
  <li>Milk</li>
</ul>

</body>
</html>
输出将是这样的

Unordered List with Square Bullets    
 - Coffee
 - Tea
 - Milk

否则,您必须使用webView并将html页面加载到webView将非常有效

而不是将文本视图与html一起使用,我们可以使用webView获得更有效的结果

res/values/strings.xml

<resources>
    <string name="app_name">TestProject2</string>

    <string name="html">
        <![CDATA[
        <h1>Big Title</h1>
        <p>UL list:
        <ul>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        </ul>
        <p>OL list:
        <ol>
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        </ol>
        ]]>
    </string>
</resources>

使用文本视图将html文本与所有html标记一起使用这里有一些文本格式示例@sushildlh您可以为此提供一些示例或参考代码吗?其他人的答案中已经给出了这些示例或参考代码refer@Kamlesh,我是你的个人资料。你为什么不接受任何答案???
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
String htmlAsString = getString(R.string.html); 
Spanned htmlAsSpanned = Html.fromHtml(htmlAsString);      
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadDataWithBaseURL(null, htmlAsString, "text/html", "utf-8", null);
}