按钮背景的选择器文件

时间:2012-04-16 10:08:22

标签: android-layout

任何人都可以提供选择器文件作为按钮的背景。按下按钮时背景图像会改变。我将为按钮的背景设置自定义图像。可绘制文件listbackground的内容是什么?

    

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="Button 1" 
        android:background="@drawable/listbackground"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="37dp"
        android:text="Button 2" 
        android:background="@drawable/listbackground"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button2"
        android:layout_centerVertical="true"
        android:text="Button 3" 
        android:background="@drawable/listbackground"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="48dp"
        android:text="Button 4" 
        android:background="@drawable/listbackground"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

listbackground.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:drawable="@drawable/mainfocussed"></item>
    <item android:state_pressed="true" android:drawable="@drawable/mainpressed"></item>
    <item android:drawable="@drawable/main"></item>

</selector>