Được lưu trong phần “Những gì tôi đã học được về Android ngày hôm nay”, hóa ra bạn có thể hiển thị một chuỗi HTML trong một Show Bỏ qua vấn đề đó trong giây lát. nếu bạn muốn thử hiển thị một chuỗi HTML trong một // get our html content String htmlAsString = getString(R.string.html); Spanned htmlAsSpanned = Html.fromHtml(htmlAsString); // used by TextView // set the html content on the TextView TextView textView = (TextView) findViewById(R.id.textView); textView.setText(htmlAsSpanned); Chuỗi HTML của tôiTrong mã ví dụ của tôi, tôi đã định nghĩa một <resources> <string name="app_name">TestProject2</string> <string name="html"> <![CDATA[ <h1>Main Title</h1> <h2>A sub-title</h2> <p>This is some html. Look, here\'s an <u>underline</u>.</p> <p>Look, this is <em>emphasized.</em> And here\'s some <b>bold</b>.</p> <p>This is a UL list: <ul> <li>One</li> <li>Two</li> <li>Three</li> </ul> <p>This is an OL list: <ol> <li>One</li> <li>Two</li> <li>Three</li> </ol> ]]> </string> </resources> Như được hiển thị, nếu bạn định đặt nội dung HTML thành một chuỗi trong tệp XML như thế này, bạn cần đặt nó bên trong phần tử TextView trông như thế nàoVới một tệp bố cục được xác định như thế này <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" android:gravity="top|fill_vertical"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <!-- <WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/webView" android:layout_below="@+id/helloWorld" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> --> </LinearLayout> và một <resources> <string name="app_name">TestProject2</string> <string name="html"> <![CDATA[ <h1>Main Title</h1> <h2>A sub-title</h2> <p>This is some html. Look, here\'s an <u>underline</u>.</p> <p>Look, this is <em>emphasized.</em> And here\'s some <b>bold</b>.</p> <p>This is a UL list: <ul> <li>One</li> <li>Two</li> <li>Three</li> </ul> <p>This is an OL list: <ol> <li>One</li> <li>Two</li> <li>Three</li> </ol> ]]> </string> </resources>0 được định nghĩa như thế này package com.alvinalexander.testproject2; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.widget.TextView; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get our html content String htmlAsString = getString(R.string.html); // used by WebView Spanned htmlAsSpanned = Html.fromHtml(htmlAsString); // used by TextView // set the html content on a TextView TextView textView = (TextView) findViewById(R.id.textView); textView.setText(htmlAsSpanned); // WebView webView = (WebView) findViewById(R.id.webView); // webView.loadDataWithBaseURL(null, htmlAsString, "text/html", "utf-8", null); } } chuỗi HTML được hiển thị trong tiện ích con Như bạn có thể thấy, Android WebView tốt hơnTrên thực tế, các vấn đề về màn hình đủ tệ đến mức có lẽ tốt hơn hết là bạn nên sử dụng Tóm lượcVì vậy, trong khi bạn có thể kết xuất một chuỗi HTML trong Android Ghi chú. Đối với ví dụ này, tôi đã sử dụng <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" android:gravity="top|fill_vertical"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <!-- <WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/webView" android:layout_below="@+id/helloWorld" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> --> </LinearLayout>2 của 14 và <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" android:gravity="top|fill_vertical"> <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <!-- <WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/webView" android:layout_below="@+id/helloWorld" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> --> </LinearLayout>3 của 21 và kết quả được hiển thị trong trình giả lập Nexus 5 Làm cách nào để tải chuỗi HTML trong WebView Android?Mã nguồn. . Bước 1. Tạo dự án. Tạo một dự án ứng dụng Android có tên là “WebViewLoadDataExampleApp” Bước 2. Tạo bố cục. Thay đổi độ phân giải -> bố cục -> Activity_main. xml như bên dưới Bước 3. Tạo MainActivity. . Android WebView tải ví dụ về dữ liệu html. . Bước 4. Chạy ứng dụng Làm cách nào để mở tệp HTML trong WebView Android?Phương thức WebView để tải tệp của chúng tôi cần một URI , vì vậy chúng tôi cần truy cập tệp HTML bằng URI đó. Vì chúng tôi đã lưu trữ nó trong thư mục nội dung nên chúng tôi có thể truy cập nó bằng cách sử dụng tệp . ///android_asset/{file_name} . Bây giờ hãy tải tệp đó trong MainActivity của chúng tôi.
Làm cách nào để tải tệp HTML trong Android?Bước 1. Để thêm tệp HTML cục bộ vào dự án Android của bạn, phải có một thư mục nội dung trong đó. Để tạo thư mục nội dung trong studio Android, trước tiên hãy mở dự án của bạn ở chế độ Android như trong hình bên dưới. Bước 2. Truy cập ứng dụng > nhấp chuột phải > Mới > Thư mục > Thư mục nội dung và tạo thư mục nội dung .
Làm cách nào để tải chuỗi HTML trong WebView trong Swift?Cách tải chuỗi HTML vào WKWebView hoặc UIWebView. loadHTMLString() Nếu bạn muốn tải tài nguyên từ một địa điểm cụ thể, chẳng hạn như tệp JavaScript và CSS, bạn có thể đặt tham số baseURL thành bất kỳ URL nào. |