How to make bottom border in drawable shape XML selector?

 ̄綄美尐妖づ 提交于 2019-12-19 05:34:13

问题


I'm trying to create a drawable shape with different states for my button. So I wrote this:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="@android:color/black" >
    <shape android:shape="rectangle" >
        <solid android:color="@color/NEGATIVE_pressed" />
        <stroke
            android:width="1dp"
            android:color="@color/ORANGE" />
        <corners android:radius="4dp" />
    </shape>
</item>
<item android:state_focused="true" android:color="@android:color/black" >
    <shape android:shape="rectangle" >
        <solid android:color="@color/NEGATIVE_focused" />
        <stroke
            android:width="1dp"
            android:color="@color/ORANGE" />
        <corners android:radius="4dp" />
    </shape>
</item>
<item android:color="@android:color/black" >
    <shape android:shape="rectangle" >
        <solid android:color="@color/NEGATIVE" />
        <stroke
            android:width="1dp"
            android:color="@color/NEGATIVE" />
        <corners android:radius="4dp" />
    </shape>
</item>
</selector>

Then in my button I use it as android:background="@drawable/btn_negative_selector"

However, I want to draw a bottom border to that shape, to be, something like 3 dp and of different color, but I can't figure out how to do it. I tried searching, but didn't find anything suitable for selector. Any suggestions, please?


回答1:


First I'm separating the shapes to make them easier to manage.

This is your btn_negative_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@xml/rectangle_button_pressed" android:state_pressed="true"></item>
    <item android:drawable="@xml/rectangle_button_focused" android:state_focused="true"></item>
    <item android:drawable="@xml/rectangle_button" ></item>
</selector>

create folder called 'xml' in your res and save these shapes into it:

1) rectangle_button_pressed:

    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
        <solid android:color="@color/NEGATIVE_pressed" />
        <stroke
            android:width="1dp"
            android:color="@color/ORANGE" />
        <corners android:radius="4dp" />
    </shape>

2) rectangle_button_focused:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
        <solid android:color="@color/NEGATIVE_focused" />
        <stroke
            android:width="1dp"
            android:color="@color/ORANGE" />
        <corners android:radius="4dp" />
    </shape>

3) This one rectangle_button.xml will have a border at the bottom of it by defining a shape using <layer-list>. first <item> is bottom layer and last <item> is the top layer.

<?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="@color/gray"/>
            <corners android:radius="4dp"/>
        </shape>
    </item>
    <item android:bottom="3dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/orange" />
            <corners android:radius="4dp"/>
        </shape>

    </item>
</layer-list>


来源:https://stackoverflow.com/questions/25295454/how-to-make-bottom-border-in-drawable-shape-xml-selector

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