形状可绘制为背景,底部有一条线

Zoo*_*Way 57 android shape drawable

我使用drawable作为TextView的背景,只是在文本下面有一个分隔线.用这个drawable-xml来解决它:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape 
        android:shape="rectangle">
            <solid android:color="#FFDDDDDD" />
            <gradient 
                android:angle="0"
                android:startColor="#FFAAAAAA"
                android:endColor="#FFEEEEEE"
                />
        </shape>
    </item>

    <item android:bottom="2dp">
        <shape 
        android:shape="rectangle">
            <solid android:color="#FF000000" />
        </shape>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

但是这种方法在黑色矩形上方绘制一个彩色矩形.我想只在形状底部的线条没有黑色矩形,因为黑色不透明.我怎么能实现这一目标?

Go *_*man 196

这就是我在底部获得一条线的方式.绘制一个笔划,然后将项目向上移动到两侧以使顶部和侧面不显示笔划:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-8dp" android:left="-8dp" android:right="-8dp">
        <shape> 
            <solid android:color="#2b7996"/>
            <stroke android:color="#33b5e5" android:width="6dp"/>
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

  • 摆脱`<solid android:color ="#2b7996"/>`给了我想要的结果,只有下划线(至少在Galaxy S4(Kit Kat)上......不确定其他设备). (3认同)

小智 33

我认为这是更好的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:gravity="bottom">
        <shape>
            <size android:height="1dp" />
            <solid android:color="#000000" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案适用于除三星Lollipop设备以外的几乎所有设备。整个视图是用`solid`标签中提供的颜色绘制的。由于三星的市场份额,这一点不容忽视。 (2认同)

dmo*_*mon 32

一般情况下,除非绝对必要,否则我会尝试尽可能少地使用背景,因为这样做会覆盖具有聚焦,按下等状态的默认背景颜色.我建议只使用附加视图(在垂直LinearLayout中),即尽可能厚实.例如:

 <View 
       android:background="#FF000000" 
       android:layout_height="2dp" 
       android:layout_width="fill_parent"/>
Run Code Online (Sandbox Code Playgroud)

  • 嗯,我不太相信你的解决方案,因为它需要一个额外的布局和一个额外的视图,两个都是重型物体只是为了水平线...如果你们对背景过敏,为什么不覆盖onDraw(),只是画一个线? (7认同)

Lan*_*tka 6

通常对于类似的任务 - 我创建了像这样的图层列表drawable:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/underlineColor"/>
    </shape>
</item>
<item android:bottom="3dp">
    <shape android:shape="rectangle">
        <solid android:color="@color/buttonColor"/>
    </shape>
</item>
Run Code Online (Sandbox Code Playgroud)

我们的想法是首先用underlineColor绘制矩形,然后在这个上面绘制另一个带有实际buttonColor但使用bottomPadding的矩形.它总是有效的.

但是当我需要让buttonColor变得透明时,我无法使用上面的drawable.我找到了另一个解决方案

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>
    </shape>
</item>

<item android:drawable="@drawable/white_box" android:gravity="bottom" android:height="2dp"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

(正如你在这里看到的,mainButtonColor是透明的,而white_box只是一个简单的矩形,可以用白色Solid绘制)