Android 显示 WebView ,加载URL 时,向webview的 header 里面传递参数

1.主要布局

<?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>

2.WebViewActivity代码

package com.webview.demo;
  
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.webkit.WebView;
  
  import java.util.HashMap;
  import java.util.Map;
  
 public class WebViewActivity extends AppCompatActivity {
 
     private WebView webView ;
 
     private String webViewHeaderKey = "tokenId" ;
     private String webViewHeaderValue = "562142" ;
 
     private String url = "" ;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
 
         webView = (WebView) findViewById( R.id.webview );
 
 
         if ( webViewHeaderValue != "" ){
             Map<String, String > map = new HashMap<String, String>() ;
             map.put( webViewHeaderKey , webViewHeaderValue ) ;
 
             webView.loadUrl( url  , map ) ;
         }else {
             webView.loadUrl( url ) ;
         }
 
     }
 }


 3.Webview显示不全是加入以下代码即可:

   WebSettings settings = webView.getSettings();
   settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
   settings.setJavaScriptEnabled(true);  //这行重点  设置WebView是否允许执行JavaScript脚本,默认false,不允许



WebSettings webSettings = webView.getSettings();
//设置是否支持缩放
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
//设置是否显示缩放按钮
webSettings.setDisplayZoomControls(true);
//设置WebView是否允许执行JavaScript脚本,默认false,不允许
webSettings.setJavaScriptEnabled(true);
//设置自适应屏幕宽度
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);

猜你喜欢

转载自www.cnblogs.com/monkey0928/p/9751079.html