android给textView 设置selector没有效果

pokerWu 发布于 2015/07/15 12:20
阅读 8K+
收藏 0
我给textView 设置selector没有效果下面是layout,Relative和下面的三个TextView的background就是设置的selector,我在代码中实现了他们的监听的。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorWhite"/>
    <item android:state_pressed="true" android:drawable="@color/pressedColor"/>
    <item android:state_focused="true" android:drawable="@color/pressedColor"/>
</selector>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/user_info_select"
        android:layout_width="match_parent"
        android:layout_height="@dimen/userHeadLayoutHeight"
        android:layout_marginTop="@dimen/defaultMargin"
        android:background="@drawable/user_about_uer_style"
        android:clickable="true"
        >
        <ImageView
            android:id="@+id/user_head_img"
            android:layout_width="@dimen/headImgSize"
            android:layout_height="@dimen/headImgSize"
            android:src="@mipmap/head_img"
            android:layout_marginLeft="@dimen/defaultMargin"
            android:layout_marginTop="@dimen/paddingTopAndBottom"
            android:layout_marginBottom="@dimen/paddingTopAndBottom"
            />
        <TextView
            android:id="@+id/user_nick_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/default_nick_name"
            android:layout_toRightOf="@+id/user_head_img"
            android:layout_marginLeft="@dimen/defaultMargin"
            android:layout_marginTop="@dimen/defaultMargin"
            android:textSize="22sp"
            android:textStyle="bold"
            />
        <TextView
            android:id="@+id/user_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/defaultEmail"
            android:layout_toRightOf="@+id/user_head_img"
            android:layout_marginLeft="@dimen/defaultMargin"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="@dimen/defaultMargin"
            android:textSize="@dimen/userEmailTextSize"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/goto_icon"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/defaultMargin"
            />
    </RelativeLayout>
    <TextView
        android:id="@+id/user_setting_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/settings"
        android:drawablePadding="@dimen/defaultPadding"
        android:textSize="@dimen/barTitleSize"
        android:layout_marginTop="@dimen/viewMargin"
        android:paddingLeft="@dimen/defaultPadding"
        android:drawableRight="@mipmap/goto_icon"
        android:paddingRight="@dimen/defaultPadding"
        android:text="@string/setting"
        android:gravity="center_vertical"
        android:background="@drawable/user_about_uer_style"
        android:paddingTop="@dimen/paddingTopAndBottom"
        android:paddingBottom="@dimen/paddingTopAndBottom"
        android:clickable="true"
        />
    <TextView
        android:id="@+id/user_update_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/update_app"
        android:drawablePadding="@dimen/defaultPadding"
        android:textSize="@dimen/barTitleSize"
        android:paddingLeft="@dimen/defaultPadding"
        android:text="@string/update"
        android:gravity="center_vertical"
        android:background="@drawable/user_about_uer_style"
        android:paddingTop="@dimen/paddingTopAndBottom"
        android:paddingBottom="@dimen/paddingTopAndBottom"
        android:clickable="true"
        />
    <TextView
        android:id="@+id/user_exit_textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/logout"
        android:drawablePadding="@dimen/defaultPadding"
        android:textSize="@dimen/barTitleSize"
        android:layout_marginTop="@dimen/viewMargin"
        android:paddingLeft="@dimen/defaultPadding"
        android:paddingRight="@dimen/defaultPadding"
        android:text="@string/logout"
        android:gravity="center_vertical"
        android:background="@drawable/user_about_uer_style"
        android:paddingTop="@dimen/paddingTopAndBottom"
        android:paddingBottom="@dimen/paddingTopAndBottom"
        android:clickable="true"
        />
</LinearLayout>



加载中
0
monroe
monroe

```

<item android:drawable="@color/colorWhite"/>

```

这一句放在最后。

pokerWu
pokerWu
感谢,成功了!请问这是为什么?
0
notreami
notreami

我觉得选中应该是这个   android:state_selected   

pokerWu
pokerWu
我只需要pressed的效果,就只需要点击效果。
0
Storm-Cai
Storm-Cai
android:clickable="true"
pokerWu
pokerWu
设置了的
0
monroe
monroe

引用来自“monroe”的评论

```

<item android:drawable="@color/colorWhite"/>

```

这一句放在最后。

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.


返回顶部
顶部