项目到目前为止已完成了百分之九十,终于摸索出了android架构设计,向牛犇工程师又迈进了一大步。
下面介绍的是项目余下百分之十的webView,对于网页的加载简单粗暴,直接有效。
效果图:
1 布局xml
非常简单的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_red_bg"
>
<ImageButton
android:id="@+id/ib_webview_newsdatail_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@null"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:src="@drawable/img_menu" />
<ImageButton
android:id="@+id/ib_webview_newsdatail_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@null"
android:layout_marginLeft="5dp"
android:src="@drawable/icon_share" />
<ImageButton
android:id="@+id/ib_webview_newsdatail_textsize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/ib_webview_newsdatail_share"
android:layout_centerVertical="true"
android:background="@null"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:src="@drawable/icon_textsize" />
</RelativeLayout>
<WebView
android:id="@+id/wb_webview_newsdatail_webviewcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
2 设置跳转:
在恰当的位置进行网页跳转;
/********** 使用webView 加载网页 *********************/
Intent intent = new Intent();
intent.setClass(mActivity, WebViewActivity.class);
intent.putExtra("webview_url", mNewsList.get(position).url.replace("10.0.2.2", "192.168.56.1"));
mActivity.startActivity(intent);
/********** 使用webView 加载网页 结束 ******************/
3 webView实现
webViewActivity:
package com.example.zhihuibj;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.webview_newsdatail);
WebView mWebView = (WebView) findViewById(R.id.wb_webview_newsdatail_webviewcontent);
ImageButton mImageButton_Back = (ImageButton) findViewById(R.id.ib_webview_newsdatail_back);
ImageButton mImageButton_textsize= (ImageButton) findViewById(R.id.ib_webview_newsdatail_textsize);
ImageButton mImageButton_share =(ImageButton) findViewById(R.id.ib_webview_newsdatail_share);
//根据键值获得url
String webview_url=getIntent().getStringExtra("webview_url");//得到URL
WebSettings webview_settings = mWebView.getSettings();
//使能JavaScript
webview_settings.setJavaScriptEnabled(true);
//显示放大缩小按钮
webview_settings.setDisplayZoomControls(true);
//双击放大或缩放
webview_settings.setUseWideViewPort(true);
mWebView.setWebViewClient(new WebViewClient(){
/*
* 所有跳转的链接都会在此回调中进行调用
* */
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("跳转URL ..."+url);
view.loadUrl(url);//防止调用系统自带的浏览器打开网页
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
System.out.println("onPageStarted.... ");
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
System.out.println("onPageFinished.... ");
}
});
mWebView.loadUrl("https://www.baidu.com/");
}
}
<activity android:name=".WebViewActivity"/>