0
package com.example.webviewtheme;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {
WebView webview1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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

        String rawHTML = "<HTML>"+  
         "<body style='color: #000000; background-color: #ffffff'><h1>Hello Android </h1></body>"+  
     "</HTML>";
        webview1.setBackgroundColor(00000000);
        webview1.loadData(rawHTML, "text/HTML", "UTF-8");
    }

}

这是我的代码我想设置 webView 的背景颜色黑色和文本白色我试图在 Html 中应用但无法输出请帮助我如何在 Webview 文本和背景中设置夜间模式

4

10 回答 10

8

用这个

webview1=(WebView)findViewById(R.id.webView1);
    String rawHTML = "<HTML>"+ 
     "<head>"+"<style  type=\"text/css\">"+
     "body,h1{color: #000000;"+
     "background-color: #ffffff;}"+
     "</style></head>"+
     "<body><h1>Hello Android </h1></body>"+  
 "</HTML>";
    webview1.loadData(rawHTML, "text/html; charset=UTF-8",null);

6 月 20 日更新:

如果您想在加载完成后更改网页 css(例如使用 加载 webPageAddress 时loadUrl),您可以这样做:

  • 设置 JavaScript 启用webView

    webView.getSettings().setJavaScriptEnabled(true);
    
  • 在页面加载完成时设置监听器

    webView.setWebViewClient(new WebViewClient() {
       @Override
       public void onPageFinished(WebView view, String url) {
          // Inject CSS on PageFinished
          injectCSS();
          super.onPageFinished(view, url);
       }
    });
    
  • 注入 CSS

    private void injectCSS() {
      webView.loadUrl(
        "javascript:document.body.style.setProperty(\"color\", \"white\");"
      );
      webView.loadUrl(
        "javascript:document.body.style.setProperty(\"background-color\", \"black\");"
      );
    }
    
于 2014-04-29T07:50:23.183 回答
3

这是将Javascript注入网页以使其反转颜色的东西:

String js ="javascript: ("
                    +"function () { "

                    +"var css = 'html {-webkit-filter: invert(100%);' +"
                    +"    '-moz-filter: invert(100%);' + "
                    +"    '-o-filter: invert(100%);' + "
                    +"    '-ms-filter: invert(100%); }',"

                    +"head = document.getElementsByTagName('head')[0],"
                    +"style = document.createElement('style');"

                    +"if (!window.counter) { window.counter = 1;} else  { window.counter ++;"
                    +"if (window.counter % 2 == 0) { var css ='html {-webkit-filter: invert(0%); -moz-filter:    invert(0%); -o-filter: invert(0%); -ms-filter: invert(0%); }'}"
                    +"};"

                    +"style.type = 'text/css';"
                    +"if (style.styleSheet){"
                    +"style.styleSheet.cssText = css;"
                    +"} else {"
                    +"style.appendChild(document.createTextNode(css));"
                    +"}"

                    //injecting the css to the head
                    +"head.appendChild(style);"
                    +"}());";

                    CustomWebView.this.loadUrl(js);
于 2014-04-26T06:15:12.607 回答
2

好的,这不在文档中,也不推荐,但它会起作用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    webview.loadUrl("http://en.wikipedia.org/wiki/Lena_S%C3%B6derberg");
    try {
        Class clsWebSettingsClassic = 
            getClassLoader().loadClass("android.webkit.WebSettingsClassic");
        Method md = clsWebSettingsClassic.getMethod(
                "setProperty", String.class, String.class);
        md.invoke(webview.getSettings(), "inverted", "true");
        md.invoke(webview.getSettings(), "inverted_contrast", "1");
    } catch (Exception e) {}
}

这将对 WebView 进行反向渲染。
“inverted_contrast”的范围约为 1 到 30(不太确定)。
使用 android 4.2 测试的结果:

http://i.imgur.com/IWsRcAW.png

于 2013-09-24T09:07:49.387 回答
2

webview.setBackgroundColor(0)使 webview 透明。

您需要在 HTML 标记 bg 中设置 bg 颜色为黑色。

于 2013-09-24T06:58:06.940 回答
1

在 XML 中

<WebView
            android:id="@+id/web"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/black" />

在java代码中

String str="<div style=\'background-color:transparent;padding: 5px ;color:#white'>Enter text to display in web view</div";
WebView web=(WebView)findViewById(R.id.web);
web.loadData(str,"text/html","utf-8");
web.setBackgroundColor(Color.TRANSPARENT);
于 2013-09-24T07:02:21.833 回答
1

在java类中给出以下内容

webView1.setBackgroundColor(0);

并在 xml 中设置文本的颜色

于 2013-09-24T06:45:51.400 回答
0

You can invert colors of a WebView using its parent.

  1. You need to implement inversion in parent. I'm using a FrameLayout, but you can use some other form of layout.

    public class WebViewInverter extends FrameLayout {
         public WebViewInverter(Context context) {
            super(context);
            init();
        }
    
        public WebViewInverter(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public WebViewInverter(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        public WebViewInverter(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            init();
        }
    
        private boolean nightMode = false;
    
        private Paint paint = new Paint(); 
        private ColorFilter cf;
        private Rect inversionRect;
    
        @Override protected void dispatchDraw(Canvas c){
                inversionRect = new Rect(0, 0, getWidth(), getHeight());
                Bitmap b = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
                Canvas cc = new Canvas(b);
                super.dispatchDraw(cc);
                paint.setColorFilter(null);
                c.drawBitmap(b, 0, 0, paint);
                paint.setColorFilter(cf);
                c.drawBitmap(b, inversionRect, inversionRect, paint);
        }
    
        private void init(){
            float[] mat = new float[]
            {
                -1,  0,  0, 0,  255,
                 0, -1,  0, 0,  255,
                 0,  0, -1, 0,  255,
                 0,  0,  0, 1,  0
            };
            cf = new ColorMatrixColorFilter(new ColorMatrix(mat));
        }
    
    }
    
  2. Place Your WebView inside inverter in your layout's file:

    <your.package.name.WebViewInverter
        android:id="@+id/view_inverter"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/reader_side_margins"
        android:layout_marginTop="@dimen/reader_top_margin"
        android:layout_marginRight="@dimen/reader_side_margins"
        android:layout_marginBottom="@dimen/reader_bottom_margin">
    
        <ru.yandex.reader.widget.ReaderView
            android:id="@+id/view_reader"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </your.package.name.WebViewInverter>
    

The WebView should be inverted.

于 2015-01-29T14:38:27.520 回答
0

将此用于 webview 背景颜色:

   import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    WebView webview1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        webview1=(WebView)findViewById(R.id.webview1);

        String text = "<html><head>"
                  + "<style type=\"text/css\">body{color: #fff; background-color: #000;}"
                  + "</style></head>"
                  + "<body>"                          
                  + "Helloo Andorid"
                  + "</body></html>";

        webview1.loadData(text, "text/html", "utf-8");
        webview1.setBackgroundColor(00000000);
    }

}

它对我有用..

于 2013-09-24T06:50:08.480 回答
0

海这可以帮助你

dialogMesage.setBackgroundColor(0x00000000);

或者

myWebView.setBackgroundColor(Color.parseColor("#FFFFFF"));
于 2013-09-24T06:51:47.463 回答
-1

Android 有内置的夜间模式主题,称为Theme.AppCompat.DayNight主题

<style name="MyTheme" parent="Theme.AppCompat.DayNight"> <!-- Blah blah --> </style>

要了解更多信息,如何实现此功能,请查看以下链接

https://medium.com/@chrisbanes/appcompat-v23-2-daynight-d10f90c83e94

于 2018-02-09T09:13:48.430 回答