2012-04-19 227 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);*/