android 中带有粗体标题的多行文本

multiple lines of text with bold headings in android

我想写 2 页文本,其中包含多个段落,其中包含一些要点和一些联系电话。 请帮我提供一些参考教程或代码。我尝试使用 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 文件,然后将它们访问到您的 class 和 setText fromHtml,如下所示

 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;
    }

在您的代码中使用它,例如:

your_text_view.setText(fromHtml(getResources().getString(R.string.htmlText)));

您可以将您的文本格式化为 html 并使用以下函数将 html(带有格式)呈现给您的文本视图。

< Android 牛轧糖:

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

>= Android 牛轧糖:

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

要区分 Android 个版本,请使用

Build.VERSION.SDK_INT >= Build.VERSION_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>

将上面的代码放在 strings.xml 文件中,其中包含一些名称示例 htmlString

并像这样使用它

 textView.setText(Html.fromHtml(getResources().getString(R.string.htmlString)));

输出会像这样

Unordered List with Square Bullets    
 - Coffee
 - Tea
 - Milk

否则你必须使用 webView 并将 html 页面加载到 webview 中会非常有效

我们可以使用 webview 来获得更有效的结果,而不是将 textView 与 html 一起使用。

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>

布局文件:

<LinearLayout 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"
              android:orientation="vertical">

<WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        />
</LinearLayout>

MainActivity 如下所示:

    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);
}