2013-10-25 13 views
1

我已經發布了一個相關的問題,但部分解決了,所以在這裏我會告訴你整個代碼。無法在Java-Android中設置背景屬性兩次(圖像+顏色)

問題是我無法從白色的RelativeLayout中設置背景,例如,通過java代碼,背景資源(.PNG文件)同時設置併合並它們。

.PNG圖像是遊戲畫面的一部分,具有透明的空間。我想獲得的是以白色顯示這個背景,因爲有黑色的細節,因爲我預先建立的背景是黑色的(我猜想選擇了最初的主題),所以無法看到它。

下面的代碼對應於XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/id_act_principal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:keepScreenOn="true" 
    tools:context=".Principal" > 

而java文件:

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

     RelativeLayout fondo = (RelativeLayout) findViewById(R.id.id_act_principal); 
     fondo.setBackgroundResource(R.drawable.prototipoestructurapantalla); 

     } 

的java文件正確設置的背景圖像,但是在XML文件中設定顯示白色背景並且不顯示它。它一直是黑色的。

我希望你能幫助我。

回答

1

試試這個:
創建一個名爲image_with_white_backgroud.xml繪製:

<?xml version="1.0" encoding="utf-8"?> 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item > 
     <shape > 
      <solid android:color="@android:color/white"/> 
     </shape> 
    </item> 
    <item> 
     <bitmap 
      android:src="@drawable/prototipoestructurapantalla"/> 
    </item> 
</layer-list> 

在你的RelativeLayout與android:background="@drawable/image_with_white_backgroud"

+0

謝謝Ramaral更換android:background="@android:color/white"!這是解決方案!非常感謝你。我一直在這個問題很多天...;) – JCoder

+0

我很高興提供幫助。 – ramaral

0

一次只能有一個背景,嘗試按照原樣離開relativelayout,然後爲png插入一個ImageView。