【问题标题】:How to make bottom border in drawable shape XML selector?如何在可绘制形状 XML 选择器中制作底部边框?
【发布时间】:2014-10-07 08:40:23
【问题描述】:

我正在尝试为我的按钮创建一个具有不同状态的可绘制形状。所以我写了这个:

<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 和不同颜色的东西,但我不知道该怎么做。我尝试搜索,但没有找到适合选择器的任何内容。有什么建议吗?

【问题讨论】:

    标签: android drawable xml-drawable shapedrawable


    【解决方案1】:

    首先,我将形状分开以使其更易于管理。

    这是你的 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) 通过使用&lt;layer-list&gt;. 定义形状,这个rec​​tangle_button.xml 将在其底部有一个边框,第一个&lt;item&gt; 是底层,最后一个&lt;item&gt; 是顶层。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多