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.效果

      

      

  • 相关阅读:
    k8spod资源的基础管理操作
    k8s名称空间资源
    bzoj5011: [Jx2017]颜色
    bzoj5010: [Fjoi2017]矩阵填数
    bzoj5008: 方师傅的房子
    bzoj5007: TCP协议
    bzoj5003: 与链 5004: 开锁魔法II 5005:乒乓游戏
    bzoj5020: [THUWC 2017]在美妙的数学王国中畅游
    bzoj5006: [THUWC2017 Bipartite]随机二分图
    bzoj4480: [Jsoi2013]快乐的jyy
  • 原文地址:https://www.cnblogs.com/dukc/p/5134666.html
Copyright © 2011-2022 走看看