只有一个透明边框的形状

4
我正在尝试找出如何创建一个只有右边框透明的形状:
+ + + + + + + +
+
+
+
+ + + + + + + +

我想知道如何做到这一点。目前我只有代表矩形的基本形状,但从这一点开始,我不确定是否可能做到我想要的:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#07000000" /> <!-- Transparent background -->


    <corners
        android:topLeftRadius="10dp"
        android:bottomLeftRadius="10dp" />
    <stroke
        android:width="2dp"
        android:color="@android:color/white" />
</shape>

使用九宫格可绘制来创建那种形状。 - pskink
是的,谢谢你的回答。但实际上我在想是否有其他方法可以做到这一点。 - Alex DG
当然可以使用带有自定义形状类的ShapeDrawable,但是9patch是最简单的方法。 - pskink
2个回答

5
主要思路是隐藏您不想显示的线条。由于在shape中没有指定它的选项,因此必须使用layer-list并定义负填充来移动矩形,使右侧超出边界。最好将所有角都圆化,因为其中并非所有角都可见。尝试这段代码:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:right="-10dp">
        <shape android:shape="rectangle">
            <solid android:color="#07000000" />

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

            <stroke
                android:width="2dp"
                android:color="@android:color/white"/>
        </shape>
    </item>
</layer-list>

0
尝试下面的 XML -
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="#FF0000" />
    </shape>
</item>
<item android:left="5dp">
    <shape android:shape="rectangle">
        <solid android:color="#000000" />
    </shape>
</item>
</layer-list>

它不起作用。我只有一个左边框,顶部、底部和右边框都不可见。但还是谢谢你的想法。 - Alex DG
1
@Alex 使用九宫格可拉伸图片来实现那种形状。 - pskink

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接