android TextView selector点击样式改变

来源:互联网 发布:js获取当前时间字符串 编辑:程序博客网 时间:2024/06/07 21:52
1.selector 从单词的意思来说:选择器,就是对你的目标的控制。selector主要是用在ListView的item单击样式和TextView和Button的点击样式。
2.主要属性介绍:
android:state_selected选中
android:state_focused获得焦点
android:state_pressed点击
android:state_enabled设置是否响应事件,指所有事件
3.下面通过例子来说一下selector对TextView设置:
1).在res下创建一个drawable文件夹用于装textselector.xml  ,对点击样式的定义放在这里面
2). 将textselector.xml资源加载到TextView上
textselector.xml
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:color="#45c01a" android:state_pressed="true"/><itemandroid:color="#9a9a9a"/></selector>


activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"><TextViewandroid:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:paddingBottom="4dp" android:text="首页" android:textColor="@drawable/textselector" android:textSize="20sp"></TextView></LinearLayout>
运行程序之后要看到颜色改变的效果的话,还必须给TextView添加按钮监听事件,就算事件不取执行什么功能都必须去设置。
TextView textView = (TextView) findViewById(R.id.textview); textView.setOnClickListener(null);
阅读全文
0 0
原创粉丝点击