单击更改自定义列表项背景

时间:2015-06-12 03:21:11

标签: android

我有如下的线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dip"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:background="@drawable/hello"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="TextView" />

    </LinearLayout>

</LinearLayout>

我已将此图层列表设置为上面线性布局的背景,并期望线性布局会在触摸时相应地更改颜色

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

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#e9e9e9" />

            <corners android:radius="3dp" />
        </shape>
    </item>

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:bottom="3dp"
            android:left="1dp"
            android:right="1dp"
            android:state_pressed="true"
            android:top="1dp">
            <shape android:shape="rectangle" >
                <solid android:color="#f224ff" />

                <corners android:radius="3dp" />
            </shape>
        </item>
        <item
            android:bottom="3dp"
            android:left="1dp"
            android:right="1dp"
            android:state_selected="true"
            android:top="1dp">
            <shape android:shape="rectangle" >
                <solid android:color="#00ffff" />

                <corners android:radius="3dp" />
            </shape>
        </item>
        <item
            android:bottom="3dp"
            android:left="1dp"
            android:right="1dp"
            android:top="1dp">
            <shape android:shape="rectangle" >
                <solid android:color="#ffff00" />

                <corners android:radius="3dp" />
            </shape>
        </item>
    </selector>

</layer-list>

但没有任何反应。我做错了吗?

1 个答案:

答案 0 :(得分:0)

在drawable中创建一个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="@color/actionbar_blue_20A" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/yellow" /> <!-- pressed -->
    <item android:drawable="@android:color/green" /> <!-- default -->
</selector>

然后将其用作LinearLayout的背景