首頁 > 軟體

Android webView如何輸出自定義網頁

2020-09-23 12:00:53

這次來使用webview輸出網頁型資料。因為這樣的資料好使用富文字編輯器,有各種各樣的拓展。

上程式碼:

package controller.hzl.com.testcall;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  private WebView webView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String webs = ""<!DOCTYPE html>n" +
        "<html>n" +
        "<head>n" +
        "<meta charset="utf-8">n" +
        "<title>hello tomcat</title>n" +
        "</head>n" +
        "<body>n" +
        "  <h1>1</h1>n" +
        "  <p>2</p>" +
        "tel:13800138000" +
        "</body>n" +
        "</html>"" ;

    webView = (WebView) findViewById( R.id.webview );

    //webview
    webView.loadDataWithBaseURL( null, webs , "text/html", "UTF-8", null ) ;


  }


 
}

頁面程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
  android:fitsSystemWindows="true"
  tools:context=".MainActivity">

  <WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </WebView>

</RelativeLayout>

這裡提示一點,如果想要拿網頁進行顯示的話,需要加一行程式碼,不然顯示不了。

  </application>
  <uses-permission android:name="android.permission.INTERNET" />

程式碼完畢,放結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com