Android border bottom for custom view not working

不羁的心 提交于 2019-12-13 07:43:50

问题


What I'm trying to do is to have a relativelayout with a gradient on it and a gray bottom border, I'm using the below custom view in my application:

<?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>

and this is 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>

but the above code applies border to all borders not just bottom border. I added the separate shape xml to the relativelayout but it didn't work which I assume the view with gradient covers it maybe?? I'm quiet lost. thanks for your help.


回答1:


The android:bottom attribute is actually the "bottom offset in pixels" as noted here about layer-list drawables. There is no explicit way to create a rectangle that is stroked only on one side.

However you can use the same trick that Krylez suggested here: https://stackoverflow.com/a/19239478/1538674

So something like this:

<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>


来源:https://stackoverflow.com/questions/30279821/android-border-bottom-for-custom-view-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!