请参阅位图和状态列表drawable

时间:2011-12-23 19:38:59

标签: android xml drawable

我正在设置一个xml布局,其上有一个按钮,该按钮上有文本和图像。为了使图像显示为未拉伸,我引用位图xml文件并调整图像的重力。我还想在用户点击它时更改按钮上的图像。为此,我有一个状态列表xml文件。这两个组件在提到时都可以单独正常工作,但我似乎一次只能做到这一点。有没有办法引用多个可绘制文件,或者将它们组合成一个?

homescreen.xml

<Button
android:id="@+id/bHome"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_weight="2"
android:background="@drawable/homebutton"
android:text="Home"
android:textColor="#FFFFFF"
android:gravity="bottom|center">
</Button>    

homebutton.xml

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/homeimage"
android:gravity="center"/>

homebuttonclicked.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/homeclicked" android:state_pressed="true" ></item>
<item android:drawable="@drawable/homeclicked" android:state_focused="true"></item>
<item android:drawable="@drawable/home"></item>
</selector>

1 个答案:

答案 0 :(得分:0)

State List使您可以为所需的每个按钮状态列出几个drawable,包括正常状态和按下状态。请按照此文档资源创建正确的状态列表,并将其用作图像的背景。希望这会有所帮助。