简体   繁体   English

更改页面样式Android WebView

[英]Change page styles Android 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.. 我目前正在开发一个小型应用程序,该应用程序仅将一个网页加载到webview中,并且希望能够在使用此应用程序浏览时使该网页看起来有所不同。

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).. 此页面使用一个.css,我想使用自己的.css版本而不是原始版本,或者只用我的一部分替换原始版本(通过使用原始版本,然后用我的版本覆盖)。

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? 是否可以修改页面的源代码(甚至在加载之前可选),以便我可以更改某些样式以使网页看起来有所不同?

Just to explain: I want change <link rel="stylesheet" type="text/css" href="original.css"> to <link rel="stylesheet" type="text/css" href="original.css"> <link rel="stylesheet" type="text/css" href="http://www.something.com/edited.css"> 只是说明一下:我想将<link rel="stylesheet" type="text/css" href="original.css">更改为<link rel="stylesheet" type="text/css" href="original.css"> <link rel="stylesheet" type="text/css" href="http://www.something.com/edited.css">

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.. 顺便说一下,我正在使用Xamarin Studio,C#Android WebView项目。此外,这很有可能被称为以下内容的重复: 覆盖Android WebView中的网页样式,但从未得到答复,因此对我不是很有帮助..

Have you looked at: 您是否看过:

addJavascriptInterface (Object object, String name) of the WebView? addJavascriptInterface (Object object, String name)

You could inject code to manipulate the DOM. 您可以注入代码来操作DOM。 This probably a bad idea as it is a SECURITY ISSUE in a number of ways. 这可能是个坏主意,因为从很多方面来说这都是一个安全问题。

Less intrusive and more secure, the WebSettings object allows you to change some presentation aspects: http://developer.android.com/reference/android/webkit/WebSettings.html WebSettings对象减少了干扰,提高了安全性,可让您更改某些表示方面: http : //developer.android.com/reference/android/webkit/WebSettings.html

In this area another tool to explore is to create and set a WebViewClient subclass: http://developer.android.com/reference/android/webkit/WebViewClient.html 在该区域中,另一个可供探索的工具是创建并设置WebViewClient子类: http : //developer.android.com/reference/android/webkit/WebViewClient.html

Just a few avenues to explore. 只是一些探索途径。

You can inject your css using Javascript. 您可以使用Javascript注入CSS。 You can inject following javascript after the WebView is finished loading: WebView加载完成后,可以注入以下javascript:

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);  

So your final Java code will look something like this: 因此,您最终的Java代码将如下所示:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据Webview的内容更改XAML页面 - Change the XAML Page based on the content of the webview 如何在Webview Windows 8.1应用程序的按钮单击上更改页面 - How to change page on button click of webview windows 8.1 app WPF WebView2 更改内容后读取相同页面 - WPF WebView2 read same page after change content 如何从WebView的页面中获取数据? Android Xamarin - How to get data from the page in WebView? Android Xamarin 带有webview savedInstanceState的Android Fragment选项卡在方向更改时始终为null - Android Fragment Tab with webview savedInstanceState always null on orientation change 如何动态更改CSS样式 - How to change the css styles dynamically UWP XAML NavigationMenu更改样式 - UWP XAML NavigationMenu Change styles 尝试在新页面上更改MainPage但WebView无法加载网站。但是当我在应用程序启动时将其设置为主页面时它是成功的 - Trying to change MainPage but WebView on the new page fails to load site. But it is successful when I set it as the main page when application starts Webview 在更改页面和显示模式时 Android 显示问题(纵向 -&gt; 横向) - Webview display problem on Android when changing page and display mode (Portrait -> Landscape) Android Webview 有时无法呈现 pdf,而是显示空白/白页 - Android Webview cannot render the pdf sometimes and shows blank/white page instead
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM