fromHtml方法在API级别28上无法正确格式化html文本

时间:2019-07-06 14:40:15

标签: java android html textview bullet-span

基本上,我正在使用Html.fromHtml方法在textView中以html格式显示文本。除列表项目的项目符号外,其他所有内容均正确呈现。就像这种情况(Bullet points in textview are cut off)一样,它们被切断了。

我在这里看到的内容(Bullet points in textview are cut off) 此处(https://medium.com/ackee/how-to-make-bulletproof-bullet-lists-in-textview-223c54fb21e6)表示BulletSpan类存在一些错误,因此在更高版本(API lvl 28)中,他们对其进行了修复。但是,在我尝试过的所有事情中,我目前有: +已安装API lvl 28 +在gradle中编译和目标SDK版本-> 28 + Gradle中的最低SDK版本-> 24 +具有android 7.0(意味着:API lvl24)的设备

它仍然显示子弹被切断

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/basicTextView"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:text="Hello World!"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

主要活动Java代码:

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import static android.text.Html.FROM_HTML_MODE_LEGACY;

public class MainActivity extends AppCompatActivity {

    private TextView textview, basicTextView;
    private Button confirmButton;
    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basicTextView = (TextView) findViewById(R.id.basicTextView);

        text = "<ul>\n" +
                "   <li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li>\n" +
                "   <li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li>\n" +
                "   <li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li>\n" +
                "   <li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li>\n" +
                "</ul>" +
                "<nav>\n" +
                "  <ul>\n" +
                "    <li><a href=\"#\">Home</a></li>\n" +
                "    <li><a href=\"#\">About</a></li>\n" +
                "    <li><a href=\"#\">Clients</a></li>\n" +
                "    <li><a href=\"#\">Contact Us</a></li>\n" +
                "  </ul>\n" +
                "</nav>" +
                "<h1>Main Title</h1>\n" +
                "        <h2>A sub-title</h2>\n" +
                "        <p>This is some html. Look, here\\'s an <u>underline</u>.</p>\n" +
                "        <p>Look, this is <em>emphasized.</em> And here\\'s some <b>bold</b>.</p>\n" +
                "        <p>This is a UL list:\n" +
                "        <ul>\n" +
                "        <li>One</li>\n" +
                "        <li>Two</li>\n" +
                "        <li>Three</li>\n" +
                "        </ul>\n" +
                "        <p>This is an OL list:\n" +
                "        <ol>\n" +
                "        <li>One</li>\n" +
                "        <li>Two</li>\n" +
                "        <li>Three</li>\n" +
                "        </ol>";

    basicTextView.setText(Html.fromHtml(text, FROM_HTML_MODE_LEGACY));
    }
}

因此,即使它应该正确呈现列表项目符号,也仍然没有。我在Android Studio中安装了正确的API lvl,所以我不知道该如何处理此问题。如果有不清楚的地方,请询问。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

因此,如果设备lvl至少为27或更高,则显然可以正常播放。我以为如果编译和目标sdk版本在gradle中足够高,即使设备的lvl较低,BulletSpan类的固定版本也将适用于该设备。这种猜测从根本上是错误的,因为将要安装的应用程序的API lvl与设备支持的API lvl相同。这意味着,如果设备支持较低的API lvl,则将安装错误的BulletSpan类的版本。