android webview html 字体大小,Android webview设置字体大小,适配屏幕,夜间模式

本文介绍了如何在Android WebView中实现夜间模式和设置字体大小。通过使用setTextZoom()函数调整字体大小,设置useWideViewPort、setLayoutAlgorithm和loadWithOverviewMode以适应屏幕,以及注入JavaScript代码实现夜间模式的色彩改变。提供了详细的代码示例。
摘要由CSDN通过智能技术生成

1.设置字体大小

主要使用的函数是

setTextZoom(int textZoom);

/**

* Sets the text zoom of the page in percent. The default is 100.

*

* @param textZoom the text zoom in percent

*/

public abstract void setTextZoom(int textZoom);

2.适配屏幕

settings.setUseWideViewPort(true);

settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);

settings.setLoadWithOverviewMode(true);

3.设置夜间模式

主要思路:

从网上获取到数据后,自己插入一段javaScript代码来改变显示的颜色

感谢网友提供的代码,链接:

http://blog.csdn.net/ifangler/article/details/39960477

有点小改动:让网页加载后直接调用函数功能,或者可以自己手动加载

javascript:window.οnlοad=function() {

css = document.createElement('link');

css.id = 'xxx_browser_2014';

css.rel = 'stylesheet';

css.href = 'data:text/css,html,body,header,div,a,span,table,tr,td,th,tbody,p,form,input,ul,ol,li,dl,dt,dd,section,footer,nav,h1,h2,h3,h4,h5,h6,em,pre{background: #333 !important;color:#616161!important;border-color:#454530!important;text-shadow:0!important;-webkit-text-fill-color : none!important;}html a,html a *{color:#5a8498!important;text-decoration:underline!important;}html a:visited,html a:visited *,html a:active,html a:active *{color:#505f64!important;}html a:hover,html a:hover *{color:#cef!important;}html input,html select,html button,html textarea{background:#4d4c40!important;border:1px solid #5c5a46!important;border-top-color:#494533!important;border-bottom-color:#494533!important;}html input[type=button],html input[type=submit],html input[type=reset],html input[type=image],html button{border-top-color:#494533!important;border-bottom-color:#494533!important;}html input:focus,html select:focus,html option:focus,html button:focus,html textarea:focus{background:#5c5b3e!important;color:#fff!important;border-color:#494100 #635d00 #474531!important;outline:1px solid #041d29!important;}html input[type=button]:focus,html input[type=submit]:focus,html input[type=reset]:focus,html input[type=image]:focus,html button:focus{border-color:#494533 #635d00 #474100!important;}html input[type=radio]{background:none!important;border-color:#333!important;border-width:0!important;}html img[src],html input[type=image]{opacity:.5;}html img[src]:hover,html input[type=image]:hover{opacity:1;}html,html body {scrollbar-base-color: #4d4c40 !important;scrollbar-face-color: #5a5b3c !important;scrollbar-shadow-color: #5a5b3c !important;scrollbar-highlight-color: #5c5b3c !important;scrollbar-dlight-color: #5c5b3c !important;scrollbar-darkshadow-color: #474531 !important;scrollbar-track-color: #4d4c40 !important;scrollbar-arrow-color: #000 !important;scrollbar-3dlight-color: #6a6957 !important;}dt a{background-color: #333 !important;}';

document.getElementsByTagName('head')[0].appendChild(css);

};

具体代码

@SuppressLint("NewApi")

public class MainActivity extends ActionBarActivity {

private WebView webView;

private WebSettings settings;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

webView = (WebView) findViewById(R.id.webView1);

webView.loadUrl("https://www.baidu.com/s?wd=android%20webview%E5%A4%9C%E9%"

+ "97%B4%E6%A8%A1%E5%BC%8F&pn=10&oq=android%20webview%E5%A4%9C%E9%97%B4"

+ "%E6%A8%A1%E5%BC%8F&tn=baiduhome_pg&ie=utf-8&rsv_idx=2&rsv_pq=923368a80"

+ "001961e&rsv_t=fea36D19IPDHXa3FGixYdeenkoMHs%2FlpzzzVmPzHfrOscX8k8r4cofKuj"

+ "1JS%2FNllB8gM&rsv_page=1");

settings = webView.getSettings();

settings.setUseWideViewPort(true);

settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);

settings.setLoadWithOverviewMode(true);

//把js.js放到assets文件夹下,apk安装后会自动保存到你的android_asset目录下

webView.setWebViewClient(new WebViewClient(){

@Override

public void onPageStarted(WebView view, String url, Bitmap favicon) {

super.onPageStarted(view, url, favicon);

String baseUrl = "file:///android_asset/js.js";

settings.setJavaScriptEnabled(true);

//获取js文本

InputStream mIs = null;

String wholeJS = null;

try {

mIs = getResources().getAssets().open("js.js");

if(mIs != null){

byte buff[] = new byte[1024];

ByteArrayOutputStream fromFile = new ByteArrayOutputStream();

FileOutputStream out = null;

do {

int numread = 0;

numread = mIs.read(buff);

if (numread <= 0) {

break;

}

fromFile.write(buff, 0, numread);

} while (true);

wholeJS = fromFile.toString();

}else{

Toast.makeText(MainActivity.this, "js加载失败", Toast.LENGTH_SHORT).show();

}

} catch (IOException e) {

e.printStackTrace();

}

//webview添加读取的js

webView.loadUrl(wholeJS);

}

});

}

int textsize = 100;

//改变字体大小

public void addTextSize(View v){

settings.setTextZoom(textsize += 10);

}

public void lessTextSize(View v){

settings.setTextZoom(textsize -= 10);

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值