2017-06-16 50 views
0

我想在android中的行分隔符上添加三个或更多的垂直颜色。它在下图中显示为蓝色,但如果我想将其分成三部分并在其中添加不同的颜色,该怎么办?我不想为此做三个单独的views。我可以在android中执行吗?Android中的行分隔符上的不同颜色

<View 
     android:background="#00bfff" 
     android:layout_width = "match_parent" 
     android:layout_height="5dp" 
/> 

enter image description here

+1

使用九个补丁可绘制 – pskink

+0

也可以使用图像作为背景查看 –

+0

@VivekMishra不,我不想添加图像。 – Amar

回答

0

溶液1

创建3图和下面的另一

溶液加一个2

创建一个.PNG背景与3不同的颜色和为您的视图设置背景

2

是的您可以通过制作一个这样的可绘制文件来实现。并给背景这个drawable来查看。

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 

<gradient 
    android:angle="0" 
    android:centerColor="#5F3D5E" 
    android:endColor="#8F76B0" 
    android:startColor="#35293D" 
    android:type="linear" /> 

<corners android:radius="0dp" /> 

</shape> 
+0

如果我想添加四种颜色怎么办? – Amar

+0

我觉得三种颜色是渐变的限制。你不能使用这种方法添加四种颜色。 –

+0

然后你的方法将不起作用。 – Amar

0

您需要添加渐变颜色才能达到该效果。只需进入drawable目录并添加一个新的.xml资源即可。然后嵌套以下内容: 选择器 项目 形状 渐变。 在渐变标签内添加startcolor,centercolor,结束颜色和角度标签。然后将其设置为您的背景视图..我不知道有什么更好的方法来做到这一点,但会更新当我做

+0

这种方法仅限于颜色范围吗? (2,3种颜色) – Amar

+0

是的。最后,我检查了它被限制为最多3种颜色..它使渐变,所以他们不是很干净剪裁不像多视图 –

2

您可以使用layer-list drawable并使用它作为您的View背景。

例子:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <solid android:color="#ff0000"/> 
     </shape> 
    </item> 
    <item 
     android:bottom="5dp"> 
     <shape> 
      <solid android:color="#00ff00" /> 
     </shape> 
    </item> 
    <item 
     android:bottom="10dp"> 
     <shape> 
      <solid android:color="#0000ff" /> 
     </shape> 
    </item> 
</layer-list> 

输出示例:

layer list example

编辑:

对于垂直线,你做了什么@convexHull说,只是right取代bottom

例子:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <solid android:color="#ff0000"/> 
     </shape> 
    </item> 
    <item 
     android:right="50dp"> 
     <shape> 
      <solid android:color="#00ff00" /> 
     </shape> 
    </item> 
    <item 
     android:right="100dp"> 
     <shape> 
      <solid android:color="#0000ff" /> 
     </shape> 
    </item> 
</layer-list> 

输出示例:

vertical lines

+0

这些颜色线是水平的。如果我垂直需要这些颜色线怎么办? – Amar

+1

这是正确的答案。垂直颜色线可以通过更改'android:bottom'到'android:right'来完成 – convexHull

+0

@Amar我编辑了我的答案。 – torque203

0

继承人,如果你想超过3种颜色的另一种方式:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="5dp" 
    android:weightSum="3"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@color/red"/> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@color/blue/> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@color/green"/> 

</LinearLayout> 

所以,如果你想要更多的颜色,只需更改linearlayout的权重并添加另一个视图。