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.适配屏幕
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);
}
}