首页 文章

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

提问于
浏览
0

我想创建一个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>

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

按下

  • 按钮:背景变为蓝色,图标为“email_selected”

  • 选择了按钮:背景变为蓝色,图标为“email_selected”

  • 默认:背景变为灰色,图标为“电子邮件”

我试图将两个资源文件组合在一起,在背景颜色“橙色”和“灰色”的项目中使用“android:state”,但它根本不起作用 .

有人可以帮忙吗?

谢谢 .

1 回答

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

相关问题