Andriod使用webview控件往APP里内嵌网页

1.布局文件片段:res-layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.webview.MainActivity"
    tools:ignore="MergeRootFrame" >

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

</FrameLayout>

2.Java片段:src

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //WebView
        WebView browser=(WebView)findViewById(R.id.Toweb);  
        browser.loadUrl("http://www.baidu.com");  
          
        //设置可自由缩放网页  
        browser.getSettings().setSupportZoom(true);  
        browser.getSettings().setBuiltInZoomControls(true);  
          
        // 如果页面中链接,如果希望点击链接继续在当前browser中响应,  
        // 而不是新开Android的系统browser中响应该链接,必须覆盖webview的WebViewClient对象  
        browser.setWebViewClient(new WebViewClient() {  
            public boolean shouldOverrideUrlLoading(WebView view, String url)  
            {   
                //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边  
                view.loadUrl(url);  
                        return true;  
            }         
             }); 
    }  
    
    //go back
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        WebView browser=(WebView)findViewById(R.id.Toweb);  
        // Check if the key event was the Back button and if there's history  
        if ((keyCode == KeyEvent.KEYCODE_BACK) && browser.canGoBack()) {  
            browser.goBack();  
            return true;  
        }  
      //  return true;  
        // If it wasn't the Back key or there's no web page history, bubble up to the default  
        // system behavior (probably exit the activity)  
        return super.onKeyDown(keyCode, event);  
    }

3. AndroidManifest.xml 设置权限,否则无法访问

我们要想将web嵌入到app内我们需要作出xml的配置,打开上面的xml文件,在application节点下方新增这样的配置信息:

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

完整的配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.webview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.test.webview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 

</manifest>
原文地址:https://www.cnblogs.com/dekevin/p/4311086.html