2012-11-21 67 views
1

可能重複:
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> 

如何糾正呢?

回答

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>