linearlayout点击变换颜色

本文介绍了一种在Android应用中实现布局选中效果的方法。通过使用LinearLayout并配合自定义的selector,可以改变布局被选中时的颜色。文章详细展示了如何在XML布局文件中配置LinearLayout及其子元素,并解释了如何通过state_focused和state_pressed属性来控制点击反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

问题:需要设置selected的时候,代码中设置obj.setSelected(true),才会触发selected事件,待验证selected问题

转载于:https://www.cnblogs.com/wj519752831/p/4999631.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值