更改页面样式的Andr​​oid的WebView [英] Change page styles Android WebView

查看:196
本文介绍了更改页面样式的Andr​​oid的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在上简单地加载一个网页到网页视图一个小应用程序,我希望能够让这个网页看起来不同,而使用这个应用程序浏览..

I'm currently working on a small application that simply loads one webpage into webview and I want to be able to make this webpage look different while browsing using this application..

本页面使用一个.css和我想用我自己的那个版本的CSS,而不是原来的,或只是与我取代原来的部分(通过使用原,然后用我的版本覆盖它)..

This page uses one .css and I want to use my own version of that .css instead of the original one or just replace part of the original with mine (by using the original and then overwriting it with my version)..

是否有可能(甚至加载之前optionaly)来修改网页源$ C ​​$ C这样我就可以改变一些风格,使网页看起来不同?

Is it possible to modify source code of the page (optionaly before it even loads) so I can change some styles to make the webpage look different?

只是为了解释:
我想更改
<链接rel =stylesheet属性类型=文/ CSS的href =original.css>


<链接rel =stylesheet属性类型=文/ CSS的href =original.css>
<链接rel =stylesheet属性类型=文/ CSS的href =htt​​p://www.something.com/edited.css>

我使用Xamarin Studio中,C#的Andr​​oid的WebView项目,顺便说一下..
此外,这是极有可能会被调用的这个重复:<一href=\"http://stackoverflow.com/questions/23770023/override-web-page-style-in-android-webview\">Override在Android的WebView中的网页风格,但它从来就没有回答,所以它不是对我来说非常有帮助。

I'm using Xamarin Studio, C# Android WebView project, by the way.. Also, this is very likely going to be called duplicate of this: Override web page style in Android WebView but it was never answered so it's not very helpful for me..

推荐答案

您可以使用JavaScript注入你的CSS。

You can inject your css using Javascript. You can inject following javascript after the WebView is finished loading:

var link = document.createElement("link");
link.href = "http://example.com/mystyle.css";
link.type = "text/css";
link.rel = "stylesheet";
document.getElementsByTagName("head")[0].appendChild(link);  

所以,你最终的Java code会是这个样子:

So your final Java code will look something like this:

String jsToInject = "var link=document.createElement('link');link.href='http://example.com/mystyle.css';link.type ='text/css'; link.rel ='stylesheet';document.getElementsByTagName('head')[0].appendChild(link);";
myWebView.loadUrl("javascript:" + jsToInject);

这篇关于更改页面样式的Andr​​oid的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆