Android 如何在可绘制形状 XML 选择器中制作底部边框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25295454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make bottom border in drawable shape XML selector?
提问by Igal
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"
然后在我的按钮中我将它用作 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?
但是,我想为该形状绘制一个底部边框,例如 3 dp 和不同颜色的东西,但我不知道该怎么做。我尝试搜索,但没有找到任何适合选择器的东西。请问有什么建议吗?
回答by Karim
First I'm separating the shapes to make them easier to manage.
首先,我将形状分开以使其更易于管理。
This is your btn_negative_selector.xml
这是你的 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:
在您的资源中创建名为“xml”的文件夹并将这些形状保存到其中:
1) rectangle_button_pressed:
1) 矩形按钮按下:
<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:
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.
3) 这个 rectangle_button.xml 将在它的底部有一个边框,它使用<layer-list>.
第一个<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>