【问题标题】:ImageButton with image icon, colored background and border带有图像图标、彩色背景和边框的 ImageButton
【发布时间】:2015-02-09 12:20:37
【问题描述】:

我想创建一个 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”来将这两个资源文件合二为一,但它根本不起作用。

有人可以帮忙吗?

谢谢。

【问题讨论】:

    标签: android layout imagebutton


    【解决方案1】:

    好吧,我不得不使用单独的背景资源文件,并使用状态为按下和选定项目的选择器。我猜你不能在同一个资源文件中拥有“背景”和“可绘制”。这是我的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 2016-04-19
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多