- package com.example.htmltest2_webview;
- import java.lang.reflect.Field;
- import java.util.logging.LogManager;
- import android.os.Bundle;
- import android.app.Activity;
- import android.text.Html;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.View;
- import android.webkit.WebSettings.LayoutAlgorithm;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- public class MainActivity extends Activity
- {
- private WebView luntanListview;
- private String html;
- public static DisplayMetrics displayMetrics;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- displayMetrics=getApplicationContext().getResources().getDisplayMetrics();
- luntanListview = (WebView) findViewById(R.id.webview);
- html="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0\"><meta name=\"apple-mobile-web-app-capable\" content=\"yes\"><meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\"><meta content=\"telephone=no\" name=\"format-detection\"></head><P><FONT size=5>【一汽-大众诸暨元通】 —— 全心,全服务</FONT><SPAN style=\"COLOR: #000000\"><!--StartFragment --></P><DIV><FONT size=5> 9月27-29日,诸暨元通一汽-大众全系现车秋季城市广场金秋温情大酬宾啦!对一汽-大众大众汽车感兴趣的朋友亦可带着家人,邀上朋友,结伴来城市广场一汽-大众展位,我们期待您的光临</FONT><FONT size=5>!</FONT></DIV><DIV><FONT size=5>展位位置:</FONT></DIV><DIV><IMG style=\"WIDTH: 653px; HEIGHT: 388px\" height=673 src=\"/pro/iXs_Editor/uploadfile/20130925102136428.jpg\" width=1117 border=0></DIV><DIV> </DIV><DIV><FONT size=5>梦想随行,国庆献礼,诸暨元通一汽大众金秋温情大酬宾</FONT></DIV><DIV><FONT size=5></FONT> </DIV><DIV><IMG src=\"/pro/iXs_Editor/uploadfile/20130925102251145.jpg\" border=0></DIV><DIV><FONT size=4></FONT> </DIV><DIV><FONT size=4>愿我们真心优质的服务,真挚诚恳的让利,让您尽享购车之旅的那份愉悦与享受!<BR>我们的使命是让更多的人拥有一汽-大众品牌轿车!<BR></FONT><FONT size=4><STRONG><FONT color=#ff0000>汽车之路,元通相伴,汽车生活,元通服务<BR></FONT></STRONG>享受更多优惠,尽在诸暨元通一汽-大众4S店(原海越汽车)!<BR>一汽-大众授权经销商:诸暨元通汽车有限公司<BR>4S店地址:诸暨市环城西路167号(老办证中心旁)<BR>销售热线:0575-87105555<BR>预约热线:0575-87105501<BR>24小时救援热线:13857521919</FONT></DIV><P align=left><IMG src=\"http://car.zhuji.net/pro/iXs_Editor/uploadfile/20130903090510258.jpg\" border=0></P><DIV> </DIV></SPAN>";
- if (html.contains("src=\"/p"))
- {
- html = html.replace("src=\"/p", "src=\"http://car.zhuji.net/p");
- }
- Log.i("info", "=html2="+html.contains("src=\"/p") + "");
- showWebView();
- }
- private void showWebView()
- {
- // 设置WevView要显示的网页
- luntanListview.loadDataWithBaseURL(null, html, "text/html", "utf-8",
- null);
- luntanListview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
- luntanListview.getSettings().setJavaScriptEnabled(true); //设置支持Javascript
- luntanListview.requestFocus(); //触摸焦点起作用.如果不设置,则在点击网页文本输入框时,不能弹出软键盘及不响应其他的一些事件。
- // luntanListview.getSettings().setBuiltInZoomControls(true); //页面添加缩放按钮
- // luntanListview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); //取消滚动条
- // 点击链接由自己处理,而不是新开Android的系统browser响应该链接。
- luntanListview.setWebViewClient(new WebViewClient()
- {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url)
- {
- //设置点击网页里面的链接还是在当前的webview里跳转
- view.loadUrl(url);
- return true;
- }
- });
- // luntanListview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
- // @Override
- // public void onFocusChange(View v, boolean hasFocus) {
- // if (hasFocus) {
- // try {
- // // 禁止网页上的缩放
- // Field defaultScale = WebView.class
- // .getDeclaredField("mDefaultScale");
- // defaultScale.setAccessible(true);
- // defaultScale.setFloat(luntanListview, 1.0f);
- // } catch (SecurityException e) {
- // e.printStackTrace();
- // } catch (IllegalArgumentException e) {
- // e.printStackTrace();
- // } catch (IllegalAccessException e) {
- // e.printStackTrace();
- // } catch (NoSuchFieldException e) {
- // e.printStackTrace();
- // }
- // }
- // }
- // });
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event)
- {
- if ((keyCode == KeyEvent.KEYCODE_BACK) && luntanListview.canGoBack())
- {
- luntanListview.goBack();//返回webView的上一页面
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- }
Android:WebView加载Html格式字符串
最新推荐文章于 2024-04-19 11:37:32 发布