设置背景图像和xml资源

时间:2011-07-07 11:24:29

标签: android android-xml repeat

我使用以下代码来绕过RelativeLayout的角。我将其保存为mybackground.xml文件夹中的drawable 它可以很好地圆角,但问题是我还想添加透明图像作为我的RelativeLayout的背景。我怎样才能实现这两件事?如何为RelativeLayout同时使用图像和可绘制的xml(用于圆角)......

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" android:padding="10dp">
   <corners android:bottomRightRadius="30dp"
            android:bottomLeftRadius="30dp"
            android:topLeftRadius="30dp"
            android:topRightRadius="30dp" />
</shape>

4 个答案:

答案 0 :(得分:18)

使用Layer List和Item标签设置Image并使用solid标签,并将颜色设置为#AA000000为透明,如下所示

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
           android:shape="rectangle" android:padding="10dp">
      <solid android:color="#AA000000"/>
      <corners android:bottomRightRadius="30dp"
               android:bottomLeftRadius="30dp" 
               android:topLeftRadius="30dp"
               android:topRightRadius="30dp" />
    </shape>
  </item>
  <item>
    <bitmap android:src="@drawable/yourfilename"/>
  </item>
</layer-list>

答案 1 :(得分:6)

您可以使用Layer-List

就像这样

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <shape android:shape="rectangle" android:padding="10dp">
    <corners android:bottomRightRadius="30dp"
             android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
             android:topRightRadius="30dp" />
    </shape>
  </item>
  <item><!-- your transparent image --></item>
 </layer-list>

答案 2 :(得分:2)

这个有效。如果你不在你角落的角落,你将无法看到圆角。有些朋友在以前的答案中谈过这个问题。他们应该围绕他们的形象来看圆形效果。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/listviewback2">
        <shape android:shape="rectangle" android:padding="10dp">
            <corners
                android:bottomRightRadius="35dp"
                android:bottomLeftRadius="35dp"
                android:topLeftRadius="35dp"
                android:topRightRadius="35dp"/>
        </shape>
    </item>
</layer-list>

答案 3 :(得分:1)

尝试这样的事情:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item> 
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" android:padding="10dp">
            <corners android:bottomRightRadius="30dp"
                android:bottomLeftRadius="30dp" android:topLeftRadius="30dp"
                android:topRightRadius="30dp" />
        </shape>
    </item>
   <item>
       <bitmap android:src="@drawable/background"/>
   </item>
</layer-list>
相关问题