Android:向 XML 文件中的按钮添加图像背景

发布于 2024-12-11 01:07:47 字数 2037 浏览 0 评论 0原文

可能的重复:
如何设置不同状态的图像按钮背景图像?

我有以下声明如何绘制按钮(uabutton.xml):

<?xml version="1.0" encoding="UTF-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:startColor="#000000"
                android:endColor="#e9e8e9"
                android:angle="270" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:endColor="#ffffff"
                android:startColor="#b9b9b9"
                android:angle="270" />
        </shape>
    </item>

    <item android:state_enabled="false">
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:startColor="#f0aa9f"
                android:endColor="#e21f00"
                android:angle="270" />
        </shape>
    </item>

    <item>        
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:startColor="#ff8a0e"
                android:endColor="#e9e8e9"
                android:angle="270" />
        </shape>    
    </item>
</selector>

我的问题是,我想向每个按钮的背景添加图像。如果有的话,我会在上面的哪里添加图像。

该按钮被添加到 RelativeLayout 中:

<Button
    android:id="@+id/aboutualocationbtn"
    android:text="Menu"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/uabutton"/>

Possible Duplicate:
how to set image button backgroundimage for different state?

I have the following declaring how a button should be drawn (uabutton.xml):

<?xml version="1.0" encoding="UTF-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" >
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:startColor="#000000"
                android:endColor="#e9e8e9"
                android:angle="270" />
        </shape>
    </item>

    <item android:state_focused="true" >
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:endColor="#ffffff"
                android:startColor="#b9b9b9"
                android:angle="270" />
        </shape>
    </item>

    <item android:state_enabled="false">
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:startColor="#f0aa9f"
                android:endColor="#e21f00"
                android:angle="270" />
        </shape>
    </item>

    <item>        
        <shape>
            <corners
                android:radius="3dp" />
            <gradient
                android:startColor="#ff8a0e"
                android:endColor="#e9e8e9"
                android:angle="270" />
        </shape>    
    </item>
</selector>

My question is, I would like to add an image to the background of each button. Where, if anywhere, in the above would I add the image.

The button is added a to a RelativeLayout:

<Button
    android:id="@+id/aboutualocationbtn"
    android:text="Menu"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/uabutton"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-12-18 01:07:47

将其包装在 图层列表 可绘制对象中。

Wrap it in a Layer List drawable.

淡紫姑娘! 2024-12-18 01:07:47
android:background="@drawable/uabutton"

指向您的可绘制文件夹中名为 uabutton 的文件作为背景,

即 uabutton.jpg 或 uabutton.png

android:background="@drawable/uabutton"

is pointing to a file called uabutton in your drawables folder as the background

ie uabutton.jpg or uabutton.png

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文