1. 程式人生 > >webview隨scrollview一起滑動

webview隨scrollview一起滑動

1、

佈局檔案

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:background="#fafafa"
android:fitsSystemWindows="true"
android:orientation="vertical"> <include android:id="@+id/title_bar" layout="@layout/top_title_bar" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fafafa"> <LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/titleContent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/base_dimen_30" android:layout_marginTop="@dimen/base_dimen_30" android
:textColor="#646464" android:singleLine="true" android:ellipsize="end" android:layout_marginRight="@dimen/base_dimen_30" android:textSize="16sp" /> <TextView android:id="@+id/onlineTime" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/base_dimen_30" android:layout_marginTop="@dimen/base_dimen_30" android:textColor="#646464" android:textSize="16sp" /> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fafafa" android:scrollbars="none" tools:ignore="WebViewLayout" android:layout_marginTop="@dimen/base_dimen_30" android:visibility="visible" /> </LinearLayout> </ScrollView> </LinearLayout>
重要的幾個點,
tools:ignore="WebViewLayout"
xmlns:tools="http://schemas.android.com/tools"
這兩句程式碼的主要作用就是讓
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="wrap_content"這個屬性可以寫上不報錯
2、

webview簡單寫下,上面內容基本上就可以了。

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
public class MainActivity extends Activity {
private EditText myEditText1;
private EditText myEditText2;
private EditText myEditText3;
private WebView myWebView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@SuppressLint("SetJavaScriptEnabled")
private void initView() {
myEditText1 = (EditText) findViewById(R.id.edit1);
myEditText1 = (EditText) findViewById(R.id.edit2);
myEditText1 = (EditText) findViewById(R.id.edit3);
myWebView = (WebView) findViewById(R.id.content);
WebSettings webSettings = myWebView.getSettings();
// 設定WebView屬性,能夠執行Javascript指令碼
webSettings.setJavaScriptEnabled(true);
// 設定可以訪問檔案
webSettings.setAllowFileAccess(true);
// 設定支援縮放
webSettings.setBuiltInZoomControls(true);
// 載入需要顯示的網頁
myWebView.loadUrl("http://www.baidu.com");
// 設定Web檢視
myWebView.setWebViewClient(new webViewClient());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
// 設定回退
// 覆蓋Activity類的onKeyDown(int keyCoder,KeyEvent event)方法
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack(); // goBack()表示返回WebView的上一頁面
return true;
}
finish();// 結束退出程式
return false;
}
// Web檢視
private class webViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}

3、

本文自己實際應用到專案中了,學習程式碼來自:http://download.csdn.net/download/qq_30299129/9603446