WebView是我们在开发中经常使用到的一个控件。主要用来加载网页面或是Html格式的内容。其实就是在我们Android系统中网页浏览器。
在我们的Android程序中,使用WebView有什么优势呢?除了加载网页面方便以后,主要还有以下两方面的好处哦。
使用前记得添加网络权限<uses-permission android:name="android.permission.INTERNET"/>
1. 动态更新:很多app都有使用h5页面做为应用的一部分,使用WebView加载h5页面,如果页面内容有更新,只需要服务端对应的h5内容更新,我们手面端开发不需要任务改动就可以实现。
2. 兼容已有的项目,受版本影响小,可以兼容低版本项目。
基本使用
1. 加载网页面,这基本最简单的方法,loadUrl(String url);
public class MainActivity extends Activity {
private WebView test_wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.test_wv=(WebView) findViewById(R.id.test_wv);
test_wv.loadUrl("http://www.sina.com");//此时运行,在手机中会打开浏览器,在浏览器中开打网页面,而不会在我们的应用中直接打开网页面
//要直接在我们的应用Activity中打开网页面,我们需要实现以下方法:
test_wv.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);//写了这个方法,就是告诉应用,用当前应用中的WebView直接加载网页面
return super.shouldOverrideUrlLoading(view, url);
}
});
}
}
布局界面也非常简单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/test_wv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp" />
</RelativeLayout>
2. 自定义WebView所在页面的标题
----------
首先在布局中新增title布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ldm="http://schemas.android.com/apk/res/com.ldm.learn"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/title_rela"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="48dp" >
<TextView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:text="返回" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="标题" />
<TextView
android:id="@+id/fresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:text="刷新" />
</RelativeLayout>
<WebView
android:id="@+id/test_wv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_rela"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"/>
</RelativeLayout>
在Actiivty中OnCreate()方法中新增代码:
this.titleTv = (TextView) findViewById(R.id.title);
test_wv.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
titleTv.setText(title);//这里就是设置标题
}
});
3. 重新加载页面(刷新页面)
4. this.freshTv = (TextView) findViewById(R.id.fresh);
this.freshTv.setOnClickListener(this);
@Override
public void onClick(View v) {
if (v.getId() == R.id.fresh) {
this.test_wv.reload();//点击刷新文字,就会重新加载页面
}
}
Android学习笔记:WebView常用技巧
最新推荐文章于 2024-09-20 19:52:13 发布