版权声明:✎ 本文为博主原创文章,未经博主(老程)允许不得转载。➤ https://blog.csdn.net/weixin_41000111/article/details/79624221
前言
不管是小型的项目还是大型的项目,只要用到网路请求的最好用网页(不管嵌入项目apk中页面、网路页面、手机内页面)。H5能够减少一些项目应用的变动功能,比如我在安卓中开发一个积分商城,客户需要在服务器中页面进行添加功能即可。其他好处……….
创建webview
这里使用Android Studio 软件进行开发。这里直接创建了一个demo项目
1、新建assets 文件夹
创建此文件夹为了项目中的网页。在AS中新建 assets 文件夹,是用于存放 html、css、js、….文件得。这里没有使用到网路请求,如果使用到请求服务器的话,在清单中添加权限(下面有)。
有图有真相
创建 webview控件
注意 webView 高度必须得有,不能为 android:layout_height=”wrap_content”,不然看不到网页的显示了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 注意:下面 android:layout_width 值不能为 wrap_content -->
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
</LinearLayout>
脚本部分
本人是使用最新的 android 8 版本开发。所以只有小部分不一样。
package cn.lolku.laocheng;
import android.app.Activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;
/**
* Created by cheng on 2018/4/17.
*/
public class demo extends Activity {
private WebView web;
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.demo);
// 1、获取控件id
web = findViewById(R.id.web);
}
}
WebView 对象属性和方法
loadUrl()
更新目录
2018-4-17 更新webview 创建 和加载三种方式。