Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何使用CSS在webview中显示图像_Html_Css_Android Studio_Media - Fatal编程技术网

Html 如何使用CSS在webview中显示图像

Html 如何使用CSS在webview中显示图像,html,css,android-studio,media,Html,Css,Android Studio,Media,在一个只显示网页的Android应用程序中,我使用JS注入在webview上应用自定义CSS。 我不可能编辑HTML。 使用CSS,我想在页面上添加一个图像,其中的背景图像位于伪元素前面的a::before 我在Chrome上用样式编辑器扩展试用了这个,效果很好!但是当在androidstudio中应用相同的CSS时,所有的代码都可以工作,但是图像不会显示 在网页HTML的一个元素上,我应用了::before伪元素。 ::before有一个宽度和一个高度,我设置了背景图像:('myurltoth

在一个只显示网页的Android应用程序中,我使用JS注入在webview上应用自定义CSS。 我不可能编辑HTML。 使用CSS,我想在页面上添加一个图像,其中的背景图像位于伪元素前面的a::before

我在Chrome上用样式编辑器扩展试用了这个,效果很好!但是当在androidstudio中应用相同的CSS时,所有的代码都可以工作,但是图像不会显示

在网页HTML的一个元素上,我应用了::before伪元素。 ::before有一个宽度和一个高度,我设置了背景图像:('myurltothepic')

这在桌面Chrome上工作,图像显示

在androidstudio上,除了URL中的背景图像外,所有注入的CSS都按预期工作

CSS:

爪哇:

没有错误消息,但URL中的图像似乎根本没有加载

我需要帮助,提前谢谢

&:before{
    background: url("http://guillaume.work/assets/images/showdown/showdownThk.png") !important;
    content: "";
    width: 80%;
    height: 130px;
    display: block;
    position: relative;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview =(WebView)findViewById(R.id.webView);
    webview.setWebViewClient(new WebViewClient(){

        String currentUrl;

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            currentUrl = url;

            if (url.startsWith("http:") || url.startsWith("https:")) {
                return false;

            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {


            // Inject CSS when page is done loading
            injectCSS();

            super.onPageFinished(view, url);
        }
    });

    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setAppCacheEnabled(true);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
    webview.getSettings().setLoadsImagesAutomatically(true);
    webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

    webview.loadUrl("https://play.pokemonshowdown.com");
}

// Inject CSS method: read style.css from assets folder
// Append stylesheet to document head
private void injectCSS() {
    try {
        InputStream inputStream = getAssets().open("showdownFancy.css");
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        inputStream.close();
        String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
        webview.loadUrl("javascript:(function() {" +
                "var parent = document.getElementsByTagName('head').item(0);" +
                "var style = document.createElement('style');" +
                "style.type = 'text/css';" +
                // Tell the browser to BASE64-decode the string into your script !!!
                "style.innerHTML = window.atob('" + encoded + "');" +
                "parent.appendChild(style)" +
                "})()");
    } catch (Exception e) {
        e.printStackTrace();


    }
    ;
}