源不在图像按钮中显示

时间:2012-09-06 07:44:21

标签: android image background imagebutton

我正在尝试制作一个会改变状态的imageButton。所以我制作了一个名为“btn_marisa”的xml文件:

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

<item android:drawable="@drawable/btn_marisa_bg_on" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_marisa_bg"/> <!-- default -->

</selector>

比我使用ImageButton做了一个布局,那就是代码:

<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageButton2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/imageButton2"
    android:adjustViewBounds="true"
    android:src="@drawable/marisa_logo"
    android:background="@drawable/btn_marisa"
    android:scaleType="fitStart" />

出于某种原因,在eclipse(在图形布局中)看起来不错,但是当我在手机上安装它时,只显示背景。它甚至改变了状态,但我没有看到我需要在背景上看到的图像。它是一个带有图标和背景的按钮,我只想让背景改变状态。 我错过了什么?谢谢!

2 个答案:

答案 0 :(得分:1)

它应该有用。

但首先,请确保源图像@drawable/marisa_logo小于背景图像btn_marisa_bg_on/btn_marisa_bg,否则图像将与整个背景重叠,您将无法看到背景状态会改变。

此外,请尝试删除此属性android:scaleType="fitStart",因为这将拉伸源图像,并且您将无法再看到背景。

答案 1 :(得分:0)

尝试使用Button而不是ImageButton。 删除android:src并只留下android:background。 在大多数情况下,这应该足够了。 试一试,看看它是否显示你想要的东西。

如果您真的想在ImageButton中想要背景和来源,请先查看我的建议是否有效,然后逐个添加修改。

相关问题