Android使用selector修改TextView中字体颜色和背景色的方法

所属分类: 软件编程 / Android 阅读数: 1038
收藏 0 赞 0 分享

本文实例讲述了Android使用selector修改TextView中字体颜色和背景色的方法。分享给大家供大家参考,具体如下:

android中的selector大家都很熟悉了,用它可以很方便的实现,控件在不同的动作中,颜色等值的变化。这里我说一下TextView中的一些应用。

我想大家都知道,Button按钮在源码上看是一种特殊的TextView,所以我们很多时候,按钮全是使用的TextView来完成,只要加一个android:clickable="true"就可以了。

TextView在使用selector时,会有两种情况,一种就是正常的在TextView控件上来判断按下,焦点等动作的判断,另一种则是TextView外层控件的这些动作,然后将动作传回TextView.

一,正常的在TextView控件上来判断按下,焦点等动作的判断

这种相对简单,一般要修改控件的背景色和文字色,我们要用到两个xml文件。代码如下:

tbackground.xml 修改背景

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 默认时的背景图片-->
  <!--<item android:drawable="@color/white" />-->
  <!-- 没有焦点时的背景图片 -->
  <item android:state_window_focused="false" android:drawable="@color/white" />
  <item android:state_focused="false" android:state_pressed="true"  android:drawable="@color/btnbackBlue" />
</selector>

这里要说明一点,大家看到了,我把默认时的背景图片(颜色)给注了,这是为什么呢,因为你把这条放在最前面,无论什么时候,它都会最先运行,它运行完了,程序就不会再往下运行了,所以下面写的全都没有了。如果你想设置默认值,请把这行代码,放到最下面。

ttextcolor.xml 修改文字

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 没有焦点时的背景图片 -->
  <item android:state_window_focused="false" android:color="@color/black" />
  <item android:state_focused="false" android:state_pressed="true"  android:color="@color/white" />
  <!-- 默认时的背景图片-->
  <item android:color="@color/black" />
</selector>

文字的修改我就把默认值,放到了最后,这里也要说一下,背景我们要用android:drawable而文字的颜色要使用android:color,不然会报错,为什么?大家想想。哈哈。。。。

<TextView
    android:id="@+id/txt_collection_cancel"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:text="取消"
    android:textColor="@drawable/ttextcolor"
    android:gravity="center"
    android:background="@drawable/tbackground"
    android:clickable="true"/>

二,TextView外层控件的这些动作,然后将动作传回TextView.

这种情况也常出现,我们一般会在外层加一个LinearLayout或是RelativeLayout。而我们会把点击的事件,给这个外层控件。这时候,你要修改的就是外层控件的背景,和TextView控件的文字颜色。这个时候,我们还用上面的方式,你会发现,TextView没有反应,为什么,因为它没有得到事件,这个时候,会用到一个属性就是android:duplicateParentState

它的官方解释是”如果设置此属性,将直接从父容器中获取绘图状态(光标,按下等)。 注意仅仅是获取绘图状态,而没有获取事件,也就是你点一下LinearLayout时Button有被点击的效果,但是不执行点击事件“。看下面的代码:

<RelativeLayout
    android:id="@+id/rela_collection_add"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:background="@drawable/tbackground"
    android:clickable="true">
    <View
      android:id="@+id/line_collection_add"
      android:layout_width="fill_parent"
      android:layout_height="1dp"
      android:background="@color/gray"
      android:layout_gravity="center_vertical"
      android:layout_alignParentBottom="true"
      />
    <TextView
      android:id="@+id/txt_collection_add"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:text="新建收藏夹"
      android:textColor="@drawable/ttextcolor"
      android:textSize="@dimen/ActionBar_title_size"
      android:duplicateParentState="true"
      android:gravity="center"
      android:layout_above="@+id/line_collection_add"
      />
</RelativeLayout>

我们在修改外层控件背景的同时,也在修改 TextView文字的颜色.

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多