2012-06-14 35 views
0

我试图使可绘制应该看起来像在下面的图像中: 3个水平线的1dp,每个具有不同的颜色,其余的空间应该是用渐变填充:形状线总是在xml可绘制的黑色

The grey part represent the gradient

所以我写了这一点:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="line"> 
      <stroke android:width="1dp" android:color="#c80047"/> 
     </shape> 
    </item> 
    <item android:top="1dp"> 
     <shape android:shape="line" > 
      <stroke android:width="1dp" android:color="#5ec800"/> 
     </shape> 
    </item> 
    <item android:top="2dp"> 
     <shape android:shape="line" > 
      <stroke android:width="1dp" android:color="#ffffff"/> 
     </shape> 
    </item> 
    <item android:left="0dp" android:right="0dp" android:top="3dp"> 
     <shape android:shape="rectangle"> 

      <gradient android:angle="-90" android:startColor="#3B3B3B" 
       android:endColor="#000000" /> 

     </shape> 
    </item> 

</layer-list> 

梯度绘制正确,但3线保持为黑色。我试图添加<stroke android:color="a_color" />而没有结果。

我错过了什么?

回答

1

那些画线,但他们在那里你认为他们是顶不绘制,相反,他们是在绘制的中心绘制的(你可以看到这一点,如果你移动的最后一个项目,用矩形任何线条形状之前顶部的渐变)并由最后一个项目(渐变矩形)覆盖。

要作出这样的绘制,你可以试试这个:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item> 
     <shape> 
      <solid android:color="#c80047" /> 

      <size android:height="1dp" /> 
     </shape> 
    </item> 
    <item android:top="1dp"> 
     <shape> 
      <solid android:color="#5ec800" /> 

      <size android:height="1dp" /> 
     </shape> 
    </item> 
    <item android:top="2dp"> 
     <shape> 
      <solid android:color="#ffffff" /> 

      <size android:height="1dp" /> 
     </shape> 
    </item> 
    <item 
     android:left="0dp" 
     android:right="0dp" 
     android:top="3dp"> 
     <shape android:shape="rectangle" > 
      <gradient 
       android:angle="-90" 
       android:endColor="#000000" 
       android:startColor="#3B3B3B" /> 
     </shape> 
    </item> 

</layer-list> 
+0

谢谢您的回答。用你的解决方案,第二行是2dp宽,而第一行和第三行是1dp。我有使用shape =“矩形”的同样的问题。从我的应用程序中提取的示例可以在这里看到:http://hpics.li/437e8e4(保存并放大) – grunk

+0

@grunk我已经看过图片,但我认为这是因为这些行的大小(请记住'1dp'对于手机屏幕来说确实很小,用户几乎不会看到那里有什么东西,更不用说看每一行的大小了)。无论如何,我测试过我的代码,如果你使用更大的尺寸(我已经使用10dp来更好地看到线条),那么线条的尺寸是相同的,并且它可以工作,线条具有相同的高度。 – Luksprog

+0

你有什么想法吗? :HTTP://stackoverflow.com/questions/19166342/how-do-i-avoid-double-borders-between-lists/19201562 noredirect = 1#comment28459924_19201562 –

0

所有这三行都在中心对齐,我看不到任何参数来设置重力。 我会去那些行也rectange。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#c80047"/> 
     </shape> 
    </item> 
     <item android:top="1dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#5ec800"/> 
     </shape> 
    </item> 
    <item android:top="2dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#ffffff"/> 
     </shape> 
    </item> 
    <item android:left="0dp" android:right="0dp" android:top="3dp"> 
     <shape android:shape="rectangle"> 
      <gradient android:angle="-90" android:startColor="#3B3B3B" 
       android:endColor="#000000" /> 
     </shape> 
    </item> 
</layer-list>