Android 自定义视图的底部边框不起作用

Android border bottom for custom view not working

我想要做的是有一个带有渐变和灰色底部边框的相对布局,我在我的应用程序中使用以下自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<ImageView 
    android:id="@+id/list_item_image"
    android:layout_width="match_parent"
    android:layout_height="@dimen/SIZE_LIST_ITEM_LARGE"
    android:scaleType="centerCrop"/>

<View
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/shape_list_item_gradient" />

<com.rahil.widgets.CustomTextView 
    android:id="@+id/list_item_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:gravity="bottom"

    android:textColor="@color/COLOR_BLACK"/>

</RelativeLayout>

这是shape_list_item_gradient:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <gradient
            android:startColor="#FFFFFFFF"
            android:endColor="#00FFFFFF"
            android:angle="90" />
    </shape>
</item>
<item android:bottom="2dp">
    <shape android:shape="rectangle">
        <stroke android:width="2dp" android:color="@color/COLOR_GRAY" />
    </shape>
</item> 
</layer-list>

但上面的代码适用于所有边框,而不仅仅是底部边框。我将单独的形状 xml 添加到 relativelayout 但它不起作用,我假设渐变视图可能覆盖它?我安静地迷路了。感谢您的帮助。

android:bottom 属性实际上是 "bottom offset in pixels" 关于 layer-list 可绘制对象的 here。没有明确的方法来创建仅在一侧描边的矩形。

不过,您可以使用 Krylez 此处建议的相同技巧:

所以像这样:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#FFFFFFFF"
                android:endColor="#00FFFFFF"
                android:angle="90" />
        </shape>
    </item>
    <item android:top="-3dp" android:right="-3dp" android:left="-3dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="2dp" android:color="@color/COLOR_GRAY" />
        </shape>
    </item>
</layer-list>