2015-09-25 68 views
1

我有列表项选择的形状(矩形):如何使用图像作为背景的形状?

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
<solid android:color="#eef0f3" /> 
<corners 
    android:bottomLeftRadius="4dp" 
    android:bottomRightRadius="4dp" 
    android:topLeftRadius="4dp" 
    android:topRightRadius="4dp" /> 
</shape> 

它的颜色#eef0f3矩形,但我需要使用背景图片来代替:

<solid android:color="#eef0f3" /> 

我怎样才能做到这一点?

+0

这可能帮助你http://stackoverflow.com/questions/21002224/add-a-background-image-to-shape-in​​-xml-android – Pavan

回答

3

您可以使用图层列表而不是形状。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@drawable/button_image" /> 
    <item> 
     <shape android:shape="rectangle"> 
      <corners 
       android:bottomRightRadius="4dp" 
       android:bottomLeftRadius="4dp" 
       android:topLeftRadius="4dp" 
       android:topRightRadius="4dp"/> 
     </shape> 
    </item> 

您还需要圆角添加到图像。要做到这一点,一看,

How to make an ImageView with rounded corners?