ImageButton与图像图标,彩色背景和边框

时间:2015-02-09 12:20:38

标签: android layout imagebutton

我想创建一个ImageButton可绘制资源文件,它既包含图像(不为背景展开),彩色背景和右边框和下边框。此外,我希望state_pressed,state_selected和默认状态具有不同的设置。

现在我已经设法为所有状态的图像创建xml资源文件:

<item android:state_selected="true" android:drawable="@drawable/emailselected" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/emailselected" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/emailselected" />
<item android:state_focused="true" android:drawable="@drawable/emailseleted" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/email" />

我还有背景和右边框和下边框xml资源文件:

<?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/darkgray" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:right="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/orange" />
        </shape>
    </item>

    <item
        android:bottom="1dp"
        android:right="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/gray" />
        </shape>
    </item>
</layer-list>

我想将所有这些组合在一个资源文件中。可能吗? 所以基本上我想:

  • 按下按钮:背景变为蓝色,图标为&#34; email_selected&#34;

  • 选择了按钮:背景变为蓝色,图标为&#34; email_selected&#34;

  • 默认:背景变为灰色,图标为&#34;电子邮件&#34;

我试图通过拥有&#34; android:state&#34;将两个资源文件合二为一。在背景颜色的项目&#34;橙&#34;和&#34;灰&#34;但它根本不起作用。

有人可以帮忙吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

好吧,我必须使用单独的后台资源文件,并使用带有按下和选中状态的选择器。我想你不能在同一资源文件中拥有“背景”和“可绘制”。这是我的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/darkgray" />
            </shape>
        </item>

        <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/orange" />
            </shape>
        </item>
    </layer-list>
</item>
<item android:state_focused="true" android:state_pressed="true">
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/darkgray" />
            </shape>
        </item>

        <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/orange" />
            </shape>
        </item>
    </layer-list>
</item>
<item android:state_focused="false" android:state_pressed="true">
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/darkgray" />
            </shape>
        </item>

        <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/orange" />
            </shape>
        </item>
    </layer-list>
</item>
<item android:state_focused="true">
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/darkgray" />
            </shape>
        </item>

        <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/orange" />
            </shape>
        </item>
    </layer-list>
</item>
<item android:state_focused="false" android:state_pressed="false">
    <layer-list>
        <item>
            <shape android:shape="rectangle">
                <solid android:color="@color/darkgray" />
            </shape>
        </item>

        <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp">
            <shape android:shape="rectangle">
                <solid android:color="@color/gray" />
            </shape>
        </item>
    </layer-list>
</item>