如何使绘制形状XML选择底部边框

如何使绘制形状XML选择底部边框

本文介绍了如何使绘制形状XML选择底部边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个绘制形状不同的状态对我的按钮。所以我写了这一点:

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>

然后在我的按钮,我用它作为安卓背景=@可绘制/ btn_negative_selector

不过,我想提请底部边框的造型,是,像不同颜色的3 DP和,但我无法弄清楚如何做到这一点。我试图寻找,但没有发现什么适合选择。有什么建议吗?

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?

推荐答案

首先我分开的形状,以使它们更易于管理。

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

这是你的btn_negative_selector.xml

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>

创建一个名为XML在你的资源文件夹并保存这些形状到它:

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

1)rectangle_button_ pressed:

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:

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)这其中rectangle_button.xml将在它的底部边框使用&LT定义形状;层列表&gt; &LT;项目&GT; 是底层,最后&LT;项目&GT; 是在顶层

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>

这篇关于如何使绘制形状XML选择底部边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:21