Android:向 XML 文件中的按钮添加图像背景
可能的重复:
如何设置不同状态的图像按钮背景图像?
我有以下声明如何绘制按钮(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其包装在 图层列表 可绘制对象中。
Wrap it in a Layer List drawable.
指向您的可绘制文件夹中名为 uabutton 的文件作为背景,
即 uabutton.jpg 或 uabutton.png
is pointing to a file called uabutton in your drawables folder as the background
ie uabutton.jpg or uabutton.png