zoukankan      html  css  js  c++  java
  • android的文本状态选择器-ColorStateList

      前言:我们都知道在drawable中我们可以自定义button等别的控件的选择状态,比如如果选中什么颜色,默认什么颜色,然而button的文本也能通过这种方式来进行设置

    ,这就需要ColorStateList的这个类。

      1.这个是他的文档

      2.使用方式

      ①类似的需要定义一个XML的选择器,但是item中只需指明状态和颜色

        button_text_selector.xml文件

     1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     2 
     3     <item android:state_pressed="true" android:color="#ffff0000"/>
     4  <!-- pressed -->
     5     <item android:state_focused="true" android:color="#ff0000ff"/>
     6  <!-- focused -->
     7     <item android:color="#ff000000"/>
     8  <!-- default -->
     9 
    10 </selector>

      ②在java代码中进行调用,两种方式

      

     1 Button btn = (Button) findViewById(R.id.id_btn);
     2         // 第一种方式
     3         // 导入文本的选择xml
     4         XmlPullParser xrp = getResources().getXml(
     5                 R.drawable.button_text_selector);
     6         try {
     7             ColorStateList csl = ColorStateList.createFromXml(getResources(),
     8                     xrp);
     9             btn.setTextColor(csl);
    10         } catch (Exception e) {
    11 
    12         }
    13         // 第二种方式
    14         Resources resources = getResources();
    15         ColorStateList csl = resources
    16                 .getColorStateList(R.drawable.button_text_selector);
    17 
    18         if (csl != null) {
    19             btn.setTextColor(csl);
    20         }

      3.效果

      

      

  • 相关阅读:
    有关多线程的一些技术问题
    Remoting VS WCF 传输效率对比
    中英文术语对照表
    WCF配置文件全攻略
    架构设计之分布式文件系统
    Rails性能优化简明指南 (转载)
    不要活在别人的生活里(摘自开复网)
    find 命令 使用 (转载)
    turbo C BGI 基本图形接口的 例子
    如何编写Ruby控制台程序(一)
  • 原文地址:https://www.cnblogs.com/dukc/p/5134666.html
Copyright © 2011-2022 走看看