2013-09-24 42 views
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"); 
    } 

} 

这是我的代码要设置web视图的背景颜色黑文白我试图适用于HTML,但不能出去放,请帮助我如何设置夜间模式在网页视图文本和背景如何设置背景颜色黑色和机器人的WebView文白

+0

尝试这些链接[Link](http://stackoverflow.com/questions/11225296/how-set-text-color-of-webview-in-android)和[Link](http://stackoverflow.com /问题1 ons/1254970/change-text-color-in-a-webview)和[Link](http://stackoverflow.com/questions/3389446/android-webview-changing-backcolor-and-font-color) – vish

回答

2

给在Java类

webView1.setBackgroundColor(0); 

以下,并设置文本的颜色在XML

+0

它没有工作亲爱的 – user2794306

0

用这个网页流量backg roundColor:

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

} 

它的作品对我..

+0

我有Puted另一个按钮为白天模式在其中我已经设置backGround白色我将如何做到这一点 – user2794306

+0

你必须应用背景白色为相同的网络视图? – Lokesh

0

海这可能会帮助你

dialogMesage.setBackgroundColor(0x00000000); 

myWebView.setBackgroundColor(Color.parseColor("#FFFFFF")); 
+0

Actully我想应用白天模式和夜间模式的主题设置。 – user2794306

2

webview.setBackgroundColor(0)使网页流量透明。

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

0

在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); 
+0

Actully我想应用白天模式和夜间模式的主题设置。 – user2794306

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

+0

它也适用于我...使用这种颜色的字体和其他颜色是黑色或白色...我们可以得到的颜色?我需要像webview中的夜间模式...谢谢。 –

+0

它抛出ClassNotFoundException。我看过调试器 - getSettings()返回WebSettings实例。 –

2

这里是东西,将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); 
2

使用本

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

谢谢。你节省了我宝贵的时间。 – Parthi

+0

@Parthi高兴我可以帮你:) – MHP

0

您可以使用其父项反转WebView的颜色。

  1. 您需要在父项中实施反演。我正在使用FrameLayout,但您可以使用其他一些布局。

    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. 把你的WebView变频器内部的布局的文件:

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

WebView应该被反转。