Android中的WebView实战详解(一)
一、为什么要用WebView?
1.兼容已有的项目2.可动态更新
二、WebView怎样使用?
WebView是一个控件,如在布局中设置:
<WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/relativeLayout"/>
public class MainActivity extends AppCompatActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.webView); mWebView.loadUrl("http://www.baidu.com"); //该方法作用为当一个网页跳转另一个网页时,仍然在当前webview中显示 mWebView.setWebViewClient(new WebViewClient()); }
由于程序使用到了网络功能,而访问网络需要声明权限,因此需要修改AndroidManifest.xml文件。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lance.webview">
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
三、如何自定义WebView的title?
布局设置如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.lance.webview.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout"> <Button android:id="@+id/back" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回"/> <TextView android:id="@+id/titleView" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题"/> <Button android:id="@+id/refresh" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="刷新"/> </RelativeLayout> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/relativeLayout"/> </RelativeLayout>
活动代码:
public class MainActivity extends AppCompatActivity { private WebView mWebView; private Button mBack; private TextView mTitleView; private Button mRefresh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBack = (Button) findViewById(R.id.back); mTitleView = (TextView) findViewById(R.id.titleView); mRefresh = (Button) findViewById(R.id.refresh); mBack.setOnClickListener(new MyListener()); mRefresh.setOnClickListener(new MyListener()); mWebView = (WebView) findViewById(R.id.webView); mWebView.loadUrl("http://www.baidu.com"); //该方法作用为当一个网页跳转另一个网页时,仍然在当前webview中显示 mWebView.setWebViewClient(new WebViewClient()); //该方法是接收标题 mWebView.setWebChromeClient(new WebChromeClient(){ @Override public void onReceivedTitle(WebView view, String title) { mTitleView.setText(title); super.onReceivedTitle(view, title); } }); } class MyListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()){ case R.id.back: finish(); break; case R.id.refresh: mWebView.reload(); break; default: break; } } } }
浙公网安备 33010602011771号