Android-选择器不起作用

我将其选择用于列表

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/arrow"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|left"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvDescr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" >
        </TextView>
    </LinearLayout>

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:maxHeight="20dp"
        android:minHeight="20dp" >
    </ImageView>

</LinearLayout>

和选择器arrow.xml

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

    <item><shape>
            <gradient android:angle="90.0" android:endColor="#6495ED" android:startColor="#0000FF" android:type="linear" />

            <corners android:radius="5.0dp" />
        </shape></item>
    <item android:state_focused="true"><shape>
            <gradient android:angle="90.0" android:endColor="#BA55D3" android:startColor="#800080" android:type="linear" />

            <corners android:radius="5.0dp" />
        </shape></item>
    <item android:state_pressed="true"><shape>
            <gradient android:angle="90.0" android:endColor="#BA55D3" android:startColor="#800080" android:type="linear" />

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

</selector>

结果是

 -不起作用,但是如果我删除了(以下来源),它将起作用

<item><shape>
                <gradient android:angle="90.0" android:endColor="#6495ED" android:startColor="#0000FF" android:type="linear" />

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

但这并不能给我理想的结果,因为单击“列表按钮”后,样式必须更改-但这不会发生

我要实现:

1)按图之前的样式1

2)按下按钮后,按钮样式必须更改为2张图片

3)保持这种方式,直到我单击另一个按钮

解决方法:

选择器是“状态列表可绘制”,即它“选择”< items>取决于视图的状态.

A StateListDrawable is a drawable object defined in XML that uses a
several different images to represent the same graphic, depending on
the state of the object. For example, a Button widget can exist in one
of several different states (pressed, focused, or niether) and, using
a state list drawable, you can provide a different background image
for each state.

这是语法,如docs中所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize=["true" | "false"]
    android:dither=["true" | "false"]
    android:variablePadding=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_hovered=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_activated=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

请注意您可以在< item>中设置的属性.

这是典型选择器的示例.

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
>
    <item
        android:state_focused="true" 
        android:state_pressed="false" 
android:drawable="@drawable/list_element_focused"   />
    <item
        android:state_focused="true" 
        android:state_pressed="true"
android:drawable="@drawable/list_element_focused_pressed"   />  
    <item
        android:state_focused="false" 
        android:state_pressed="true"
android:drawable="@drawable/list_element_pressed"   />  
    <item
android:drawable="@drawable/list_element_unfocused" />  
</selector>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...