使用Webview实现app启动引导页
效果如下:
首先需要一个html页面及相应的js和css支持放在assets目录下(如果没有这个目录请项目上右键-new-folder-assets)
配置权限:
<uses-permission android:name="android.permission.INTERNET"/>
新建一个类:WebViewLoadHtml,代码如下:
package com.example.webviewnavigate; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class WebViewLoadHtml extends Activity { private String url; WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //将屏幕设置为全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //去掉标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.webview); webView = (WebView)findViewById(R.id.wv_webview); url = "file:///android_asset/index.html"; loadLocalHtml(url); } @SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" }) public void loadLocalHtml(String url){ WebSettings ws = webView.getSettings(); ws.setJavaScriptEnabled(true);//开启JavaScript支持 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { //重写此方法,用于捕捉页面上的跳转链接 if ("http://start/".equals(url)){ //在html代码中的按钮跳转地址需要同此地址一致 Toast.makeText(getApplicationContext(), "开始体验", Toast.LENGTH_SHORT).show(); finish(); } return true; } }); webView.loadUrl(url); } }
然后在MainActivity中调用如下:
package com.example.webviewnavigate; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends Activity { Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = new Intent(this, WebViewLoadHtml.class); startActivity(intent); } public void onClickLoadHtml(View v){ intent = new Intent(this, WebViewLoadHtml.class); startActivity(intent); } }
新建一个空的view布局文件,名为webview,如:
<LinearLayout 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" android:orientation="vertical" > <WebView android:id="@+id/wv_webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
主页按需求设计:
<LinearLayout 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" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="这是主页" android:id="@+id/textView" /> </LinearLayout>
全部代码下载