2012-04-19 132 views
0

编辑:我当前的解决方案。将json包装在div标签中并将其设置为透明。在android中的半透明webview背景

我从远程填充json对象的webview。

{ 
"message": "<p style=\"color:#EEEEEE;background-color:transparent;font-family:'Arial Narrow','Nimbus Sans L',sans-serif;\"><\/p><p style=\"color:#EEEEEE;background-color:transparent;font-family:'Arial Narrow','Nimbus Sans L',sans-serif;\">The mission of the Canton Police Department is to protect the lives and properties of the citizens of Canton, enforce all city, state, and federal law<\/p>", 
"nationalad": "" 
} 

我可以让webview背景变得透明。但不是半透明的。当我尝试这个:

webView1.setBackgroundColor(Color.argb(128, 00, 00, 000)); 

我得到一个黑色的背景,右边有一个很小的垂直透明条。要使其透明一路:

webView1.setBackgroundColor(0x00000000); 

这里是我如何把在JSON:

try{ 
       JSONObject json = new JSONObject(result); 
       JSONArray nameArray = json.names(); 
       JSONArray valArray = json.toJSONArray(nameArray); 
       for (int i = 0; i < valArray.length(); i++) 
       {  
        WebView webView1 = (WebView) findViewById(R.id.webView1); 
        // webView1.setBackgroundColor(0x00000000); 

        //webView1.loadDataWithBaseURL(null, valArray.getString(0), "text/html", "utf-8", null); 
        webView1.setBackgroundColor(Color.argb(128, 00, 00, 00)); 
       }  

编辑:XML

<!-- Included header.xml here --> 

    <ViewStub 
     android:id="@+id/vsHeader" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inflatedId="@+id/header" 
     android:layout="@layout/header" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/cpd" 
     android:gravity="center|center_horizontal|center_vertical" 
     android:orientation="vertical" 
     android:paddingBottom="50dp" > 

     <WebView 
      android:id="@+id/webView1" 
      android:layout_width="250dp" 
      android:layout_height="300dp" 
      /> 

    </LinearLayout> 

</LinearLayout>   

enter image description here

+0

什么是您的XML布局看起来像这个页面? – 2012-04-19 18:40:45

回答

2

变化你的setBackgroundColor调用如下:

webView1.setBackgroundColor(0xAA000000); 

这应该会给你一个半透明的黑色。前两个条目是阿尔法值。值越低,颜色越透明。

+1

嗯我得到了与小条相同的黑色背景。它像这样起作用,但是黑色被放置在它上面。我很难过。我添加了一个图片 – Rick 2012-04-19 18:58:45

+0

同样的问题在这里,它适用于除WebView以外的各种Android视图 – 2012-06-12 10:36:51

4

这个工作对我来说:

网页页面主体:

background-color:transparent 

的Java活动:

webView.loadUrl("yourwebpage"); 
webView.setBackgroundColor(0x00000000); 
webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 

,你必须设置硬件加速false你的活动:

android:hardwareAccelerated="false" 
+0

如果视图不是通过XML布局文件创建的,如何将hardwareAccelerated设置为false? – 2012-11-07 15:07:38

+0

你并不需要设置hardwareAccelerated false,至少不适用于Android 4.1.1。 – 2012-11-19 07:29:14

0

只是试试这个:

webView.setAlpha(0); /*or for semi transparent : webView.setAlpha(0.5f);*/