linearlayout 点击变换颜色(点击效果)

来源:互联网 发布:mysql官网怎么下载 编辑:程序博客网 时间:2024/06/06 02:29
layout:
    <LinearLayout        android:id="@+id/llGoodCommentContainer"        android:orientation="vertical"        android:layout_weight="1"        android:gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/layout_selector"        android:clickable="true"        >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/zan"            android:focusable="false"            />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="赞!"            android:textColor="#ffffff"            android:focusable="false"            />    </LinearLayout>

2. drawable/文件夹下的layout_selecter.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/transparent_red"/>    <item android:state_pressed="true" android:drawable="@color/transparent_red" />    <item android:drawable="@color/red"/></selector>
3. color.xml文件中的两个颜色:

<color name="red">#ff5654</color><color name="transparent_red">#ddff5654</color>

说明:一开始我一直没有点击效果后来发现需要加 android:clickable="true"  才会有效果


0 0