创建一个可绘制的文件

时间:2012-11-21 06:19:38

标签: android

  

可能重复:
  creating a drawable file to make a separator

我使用以下代码在两个字段之间创建分隔符:

<View
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:background="#000000" />

<View
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:background="#FFFFFF" />

代码完美无缺,但我希望实现这样的目标:

<View
    android:layout_width="2dp"
    android:layout_height="wrap_content"
    android:background="@drawable/divider" />

这样做可以帮助我将2个视图组合成一个。但是我不知道在divider.xml文件中写入什么代码。我尝试了以下但是没有用:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
  <item >
    <shape android:shape="rectangle" >
       <stroke android:color="#000000"/>
       <size android:width="1dp" />
    </shape>
</item>
<item >
    <shape android:shape="rectangle" >
       <stroke android:color="#FFFFFF"/>
       <size android:width="1dp" />
    </shape>
</item>

我该如何纠正?

1 个答案:

答案 0 :(得分:2)

使用<layer-list> </layer-list>代替<shape/>使用多个形状进行绘制,将代码更改为:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
 <item >
    <shape android:shape="rectangle" >
       <stroke android:color="#000000"/>
       <size android:width="1dp" />
    </shape>
</item>
<item >
    <shape android:shape="rectangle" >
       <stroke android:color="#FFFFFF"/>
       <size android:width="1dp" />
    </shape>
</item>

</layer-list>
相关问题