Android开发之WebView加载html数据去除Webview滚动条的方法
生活随笔
收集整理的這篇文章主要介紹了
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="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/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="2019-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* 2020年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("https://xggapi.gfan.com/gfanmsg/read", params, new MessageContentCallBack(1));YhshNetUtils.getInstance().requestHttpPost("https://xggapi.gfan.com/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);}}} }?
總結
以上是生活随笔為你收集整理的Android开发之WebView加载html数据去除Webview滚动条的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理想汽车CEO:电池原材料将大幅降价 但
- 下一篇: Garmin佳明新一代运动智能腕表亮相