首页 > 系统 > Android > 正文

Android TextView的点击效果之改变背景颜色

2019-11-07 23:43:56
字体:
来源:转载
供稿:网友

在学习Android过程中,我们有时想要TextView有点击效果,不是文字的变化而是背景颜色的变化,那么怎么来实现呢?

下面将介绍如何实现:

1.在res目录下的drawable(此文件是自己手动创建的)文件下创建一个点击效果的文件:tv_bg_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_PRessed="true">        <shape android:shape="rectangle">            <solid android:color="#11000000"/>        </shape>    </item>     <item android:state_pressed="false">        <shape android:shape="rectangle">            <!-- 透明色 -->            <solid android:color="#00000000"/>        </shape>    </item></selector>2.在布局控件中使用

    <TextView        android:padding="5dp"        android:clickable="true"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="40sp"        android:background="@drawable/tv_bg_selector"        android:text="登录" />接下来上效果图:

             

                  点击前                                                         点击后


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表