首页 > 解决方案 > 在 Textview 中使用 HTML 标签形成一个从外部文件读取数据的表格

问题描述

我是 android 开发的新手,遇到了一个问题,我需要在TextViewusing中使用 HTML 表WebView并且也已经这样做了,但我不知道为什么在 Webview 中它只显示文件夹的最后一个文件。

这是我的 XML 文件:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/buttons"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/wallpapere"
            android:textColor="#ffff"
            android:textSize="10dp"
            tools:layout_editor_absoluteX="20dp"
            tools:layout_editor_absoluteY="26dp" />
    </LinearLayout>
</ScrollView>
<WebView
    android:id="@+id/web"
    android:layout_width="match_parent"
    android:layout_height="136dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="73dp"
    android:layout_alignParentLeft="true" />

这是我的主文件:

(File f : filesContent) {
    if (f.isFile() && f.getPath().endsWith(".txt")) try {
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line;
        int index;
        String l1 = null,l2=null,l3=null,l4=null,l5=null;

        br.readLine();

        while ((line = br.readLine()) != null) {

            if (line.contains("INITIATIVE")) {
                index = 12;
                l1 = line.substring(index, line.length());
            } else if (line.contains("DETAILS")) {
                index = 9;
                l2 = line.substring(index, line.length());

            } else if (line.contains("MAN HOUR")) {
                index = 10;
                l3 = line.substring(index, line.length());
            } else if (line.contains("AMOUNT")) {
                index = 8;
                l4 = line.substring(index, line.length());

            } else if (line.contains("NAME")) {
                index = 6;
                l5 = line.substring(index, 13);
                line.substring(index, 13));
            }
            mhtml = "<html>" + "<table>" + "<table border=1>" + "<tr>" + "<td>" + l1 + "</td>" + "<td>" + l2 + "</td>" + "<td>" + l3 + "</td>" + "<td>" + l4 + "</td>" +"<td>" + l5 + "</td>" + "</tr>" + "</table></html>";
        }
        br.close();

        webView.loadDataWithBaseURL(null,mhtml,"text/html","utf-8",null);
        webView.loadData(mhtml.toString(), "text/html", "UTF-8");
    } catch (IOException e) {
        Log.d("Exception", e.toString());
    }
}

这是我到目前为止所做的。谢谢。

标签: javaandroidhtml

解决方案


推荐阅读