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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值