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

      

      

  • 相关阅读:
    mac 格式化U盘
    SSD接口详解,再也不会买错固态硬盘了
    详解Paste deploy
    (实用)Ubuntu Linux静态IP网络配置
    Python WSGI开发随笔
    利用CA私钥和证书创建中间CA
    使用OpenSSL创建自己的CA root certificate
    创建自己的PKI公/私密钥对和公钥证书
    openssl创建自己的CA certificate
    Keystone中间件WSGI环境变量总结
  • 原文地址:https://www.cnblogs.com/dukc/p/5134666.html
Copyright © 2011-2022 走看看