2013-10-25 14 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。