android – 如何在可绘制的形状XML选择器中创建底部边框?
作者:互联网
我正在尝试为我的按钮创建一个具有不同状态的可绘制形状.所以我写了这个:
<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>
然后在我的按钮中我用它作为android:background =“@ drawable / btn_negative_selector”
但是,我想绘制一个底部边框到那个形状,就像3 dp和不同颜色,但我无法弄清楚如何做到这一点.我试过搜索,但找不到适合选择器的东西.有什么建议吗?
解决方法:
首先,我将形状分开,以便更容易管理.
这是你的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>
在res中创建名为’xml’的文件夹并将这些形状保存到其中:
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)通过使用< layer-list>定义形状,这个rectangle_button.xml将在其底部有一个边框.第一个< item>是底层和最后< item>是顶层.
<?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>
标签:android,xml-drawable,drawable,shapedrawable 来源: https://codeday.me/bug/20191007/1869427.html