【Android】linearlayout点击变换颜色

直接贴代码:

1. 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>

最后的说明, 我之前这么弄一直不出效果,后来发现,需要点击效果的Linearlayout的parent控件我设置了@color/red背景,这样的话,点击Linearlayout其实这个LinearLayout是变色的,但是由于@color/transparent_red只是@color/red变换了alpha值的颜色,所以效果被parent控件的颜色覆盖了,显示出来的没有任何效果.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章