1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android开发之WebView加载html数据去除Webview滚动条的方法

Android开发之WebView加载html数据去除Webview滚动条的方法

时间:2020-08-10 19:51:32

相关推荐

Android开发之WebView加载html数据去除Webview滚动条的方法

老套路看图:

这是通过webview加载HTML源码显示的网页:加载方法如下:

webview.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null);

设置滚动条不显示的方法有两种:

第一种:在xml中配置scrollbars为none即可

<WebViewandroid:id="@+id/wv_read_msg_content"android:layout_width="match_parent"android:layout_height="300dp"android:layout_marginBottom="10dp"android:scrollbars="none" />

第二种:Java代码设置

//设置WebView滚动条不显示//水平不显示wvReadMsgContent.setHorizontalScrollBarEnabled(false);//垂直不显示wvReadMsgContent.setVerticalScrollBarEnabled(false);

设置webview自适应的方法:

//设置网页自适应wvReadMsgContent.getSettings().setUseWideViewPort(true);wvReadMsgContent.getSettings().setLoadWithOverviewMode(true);

设置webview支持手势缩放功能

// 设置可以支持缩放wvReadMsgContent.getSettings().setSupportZoom(true);// 设置出现缩放工具wvReadMsgContent.getSettings().setBuiltInZoomControls(true);

设置后再看下效果:

WebView已自适应,WebView滚动条也隐藏了HTML数据也加载出来了

如果看着复杂我贴下完整代码:xml(缺少的资源文件请自行补全)

activity_read_message.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="3.5" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="32dp"android:layout_marginRight="32dp"android:background="@drawable/normal_login_bt_bg"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_close_read_msg"android:layout_width="15dp"android:layout_height="15dp"android:layout_gravity="right"android:layout_marginTop="15dp"android:layout_marginRight="15dp"android:src="@drawable/mch_close1" /><TextViewandroid:layout_width="match_parent"android:layout_height="25dp"android:gravity="center"android:text="消息"android:textColor="@color/login_text"android:textSize="18sp" /><Viewandroid:layout_width="match_parent"android:layout_height="0.5dp"android:layout_marginTop="5dp"android:background="@color/black_text" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@null"android:orientation="vertical"><TextViewandroid:id="@+id/tv_read_msg_title"android:layout_width="match_parent"android:layout_height="22dp"android:layout_marginLeft="15dp"android:layout_marginTop="5dp"android:layout_marginRight="15dp"android:gravity="center"android:textColor="@color/login_text"android:textSize="15sp"tools:text="我是消息标题呀我是消息标题" /><TextViewandroid:id="@+id/tv_read_msg_time"android:layout_width="match_parent"android:layout_height="15dp"android:layout_marginLeft="15dp"android:layout_marginTop="5dp"android:layout_marginRight="15dp"android:layout_marginBottom="10dp"android:gravity="center"android:textColor="@color/login_text"android:textSize="11sp"tools:text="-05-03 10:59:51" /><WebViewandroid:id="@+id/wv_read_msg_content"android:layout_width="match_parent"android:layout_height="300dp"android:layout_marginBottom="10dp"android:scrollbars="none" /></LinearLayout></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="4" /></LinearLayout>

再看ReadMessageActivity.java文件

package com.mchsdk.paysdk.activity;import android.os.Bundle;import android.view.View;import android.view.WindowManager;import android.webkit.WebSettings;import android.webkit.WebView;import android.widget.ImageView;import android.widget.TextView;import com.lidroid.xutils.exception.HttpException;import com.lidroid.xutils.http.RequestParams;import com.mchsdk.paysdk.R;import com.mchsdk.paysdk.bean.DeleteMsgBean;import com.mchsdk.paysdk.bean.GotMsgByIdParam;import com.mchsdk.paysdk.bean.MsgContentBean;import com.mchsdk.paysdk.callback.YhshNetRequestCallBack;import com.mchsdk.paysdk.utils.MCLog;import com.mchsdk.paysdk.utils.TextUtils;import com.mchsdk.paysdk.utils.YhshNetUtils;import com.mchsdk.paysdk.utils.YhshUtils;import com.xigu.gson.Gson;import org.apache.http.entity.StringEntity;import java.io.UnsupportedEncodingException;/*** 消息阅读页面** @author xiayiye5* 6月5日16:49:56*/public class ReadMessageActivity extends MCBaseActivity implements View.OnClickListener {private TextView tvReadMsgTitle;private TextView tvReadMsgTime;private WebView wvReadMsgContent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);setContentView(R.layout.activity_read_message);initView();initData();}private void initView() {ImageView ivCloseReadMsg = findViewById(R.id.iv_close_read_msg);tvReadMsgTitle = findViewById(R.id.tv_read_msg_title);tvReadMsgTime = findViewById(R.id.tv_read_msg_time);wvReadMsgContent = findViewById(R.id.wv_read_msg_content);//设置网页自适应wvReadMsgContent.getSettings().setUseWideViewPort(true);wvReadMsgContent.getSettings().setLoadWithOverviewMode(true);// 设置可以支持缩放wvReadMsgContent.getSettings().setSupportZoom(true);// 设置出现缩放工具wvReadMsgContent.getSettings().setBuiltInZoomControls(true);ivCloseReadMsg.setOnClickListener(this);}@Overridepublic void onClick(View v) {if (v.getId() == R.id.iv_close_read_msg) {finish();}}/*** 1.调用消息读取成功,2.调用获取消息内容*/private void initData() {int msgId = getIntent().getIntExtra("msgId", 0);RequestParams params = new RequestParams();GotMsgByIdParam gotMsgByIdParam = new GotMsgByIdParam();GotMsgByIdParam.BodyBean bodyBean = new GotMsgByIdParam.BodyBean();bodyBean.setId(msgId);gotMsgByIdParam.setBody(bodyBean);GotMsgByIdParam.HeaderBean headerBean = new GotMsgByIdParam.HeaderBean();headerBean.setToken(YhshUtils.getInstance().getLoginToken(this));gotMsgByIdParam.setHeader(headerBean);String json = new Gson().toJson(gotMsgByIdParam);MCLog.e("消息内容的参数", json);try {params.setBodyEntity(new StringEntity(json, "UTF-8"));} catch (UnsupportedEncodingException e) {e.printStackTrace();}params.setContentType("application/json");YhshNetUtils.getInstance().requestHttpPost("/gfanmsg/read", params, new MessageContentCallBack(1));YhshNetUtils.getInstance().requestHttpPost("/gfanmsg/info", params, new MessageContentCallBack(2));}class MessageContentCallBack implements YhshNetRequestCallBack {private int requestType;MessageContentCallBack(int requestType) {this.requestType = requestType;}@Overridepublic void onSuccess(String responseInfo) {if (requestType == 1) {MCLog.e("打印已读消息数据", responseInfo + "");DeleteMsgBean deleteMsgBean = new Gson().fromJson(responseInfo, DeleteMsgBean.class);int result = deleteMsgBean.getResult();if (result == 1) {//已阅读MCLog.e("阅读", "阅读成功!");}} else {MsgContentBean msgContentBean = new Gson().fromJson(responseInfo, MsgContentBean.class);MCLog.e("打印消息详情数据", responseInfo);//设置消息内容updateMsgContentData(msgContentBean);}}@Overridepublic void onFail(HttpException e, String s) {String localizedMessage = e.getLocalizedMessage();MCLog.e("打印异常", localizedMessage + ":" + s);}}private void updateMsgContentData(MsgContentBean msgContentBean) {MsgContentBean.ResultBean resultContent = msgContentBean.getResult();if (resultContent != null) {String htmlData = resultContent.getMessage_text();tvReadMsgTitle.setText(resultContent.getMessage_title());tvReadMsgTime.setText(resultContent.getSend_time());if (!TextUtils.isEmpty(htmlData)) {wvReadMsgContent.loadDataWithBaseURL(null, htmlData, "text/html", "utf-8", null);//数据加载后隐藏缩放按钮wvReadMsgContent.getSettings().setDisplayZoomControls(false);}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。