android实用技巧 - 用代码来实现selector

2024-06-16 13:08

本文主要是介绍android实用技巧 - 用代码来实现selector,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://my.oschina.net/non6/blog/298156
blog.csdn.net/sodino/article/details/6797821
摘要: 用代码来实现selector

  众所周知,android可以通过XML文件来创建selector,以Drawable对象的形式安装到组件上,以提供统一的风格设置。但是在某些时候,我们需要通过代码的形式来实现相同的功能,例如组件数量非常多,对应不同的图片,这时候如果还用XML的话就需要创建大量的selector文件,非常繁琐。

    例如一个TextView使用了如下的selector

<TextViewandroid:id="@+id/TextView_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:focusable="true"android:drawableTop="@drawable/selector_tabwidget_icon"android:textAlignment="center"/>

<selector xmlns:android="http://schemas.android.com/apk/res/android" ><!-- Non focused states --><item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact" /><item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" /><!-- Focused states --><item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/contact_sel" /><item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/contact_sel" /><!-- Pressed --><item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/contact_sel" /><item android:state_pressed="true" android:drawable="@drawable/contact_sel" />
</selector>

里面所引用的图片资源文件非常多,如果每个文件都对应一个XML的文件的话,就会非常繁琐,修改起来非常麻烦。

  实际上,所有XML设定能做的事情,android里同样可以用编码的方式来实现,像上面那个XML文件,就可以就下面的代码来实现:

StateListDrawable drawable = new StateListDrawable();//Non focused statesdrawable.addState(new int[]{-android.R.attr.state_focused, -android.R.attr.state_selected, -android.R.attr.state_pressed},getResources().getDrawable(R.drawable.contact));drawable.addState(new int[]{-android.R.attr.state_focused, android.R.attr.state_selected, -android.R.attr.state_pressed},getResources().getDrawable(R.drawable.contact_sel));//Focused statesdrawable.addState(new int[]{android.R.attr.state_focused,-android.R.attr.state_selected, -android.R.attr.state_pressed},getResources().getDrawable(R.drawable.contact_sel));drawable.addState(new int[]{android.R.attr.state_focused,android.R.attr.state_selected, -android.R.attr.state_pressed},getResources().getDrawable(R.drawable.contact_sel));//Presseddrawable.addState(new int[]{android.R.attr.state_selected, android.R.attr.state_pressed},getResources().getDrawable(R.drawable.contact_sel));drawable.addState(new int[]{android.R.attr.state_pressed},getResources().getDrawable(R.drawable.contact_sel));TextView textView = (TextView) findViewById(R.id.TextView_title);textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);


注意里面的“-”号,当XML的设定是false时,就需要使用资源符号的负值来设定


   
  1. package lab.sodino.statelist;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.res.ColorStateList;  
  6. import android.graphics.drawable.Drawable;  
  7. import android.graphics.drawable.StateListDrawable;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.TextView;  
  12.   
  13. /** 
  14.  * 对TextView设置ColorStateList使其在Normal、Pressed、Focused、Unable四种状态下显示不同的颜色。<br/> 
  15.  * StateListDrawable可直接使用图片应用在相似场合。 
  16.  */  
  17. public class ActColorStateList extends Activity implements OnClickListener {  
  18.     private TextView txtShow;  
  19.   
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         txtShow = (TextView) findViewById(R.id.txtShow);  
  24.         txtShow.setText("Sodino\nNormal:0xffffffff\nPressed:0xffffff00\nFocused:0xff0000ff\nUnable:0xffff0000");  
  25.         txtShow.setTextColor(createColorStateList(0xffffffff0xffffff000xff0000ff0xffff0000));  
  26.         txtShow.setOnClickListener(this);  
  27.     }  
  28.   
  29.     /** 对TextView设置不同状态时其文字颜色。 */  
  30.     private ColorStateList createColorStateList(int normal, int pressed, int focused, int unable) {  
  31.         int[] colors = new int[] { pressed, focused, normal, focused, unable, normal };  
  32.         int[][] states = new int[6][];  
  33.         states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled };  
  34.         states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused };  
  35.         states[2] = new int[] { android.R.attr.state_enabled };  
  36.         states[3] = new int[] { android.R.attr.state_focused };  
  37.         states[4] = new int[] { android.R.attr.state_window_focused };  
  38.         states[5] = new int[] {};  
  39.         ColorStateList colorList = new ColorStateList(states, colors);  
  40.         return colorList;  
  41.     }  
  42.   
  43.     /** 设置Selector。 */  
  44.     public static StateListDrawable newSelector(Context context, int idNormal, int idPressed, int idFocused,  
  45.             int idUnable) {  
  46.         StateListDrawable bg = new StateListDrawable();  
  47.         Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);  
  48.         Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);  
  49.         Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);  
  50.         Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);  
  51.         // View.PRESSED_ENABLED_STATE_SET  
  52.         bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);  
  53.         // View.ENABLED_FOCUSED_STATE_SET  
  54.         bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);  
  55.         // View.ENABLED_STATE_SET  
  56.         bg.addState(new int[] { android.R.attr.state_enabled }, normal);  
  57.         // View.FOCUSED_STATE_SET  
  58.         bg.addState(new int[] { android.R.attr.state_focused }, focused);  
  59.         // View.WINDOW_FOCUSED_STATE_SET  
  60.         bg.addState(new int[] { android.R.attr.state_window_focused }, unable);  
  61.         // View.EMPTY_STATE_SET  
  62.         bg.addState(new int[] {}, normal);  
  63.         return bg;  
  64.     }  
  65.   
  66.     @Override  
  67.     public void onClick(View v) {  
  68.         if (v == txtShow) {  
  69.             txtShow.setEnabled(false);  
  70.         }  
  71.     }  
  72. }  

这篇关于android实用技巧 - 用代码来实现selector的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1066541

相关文章

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

uniapp接入微信小程序原生代码配置方案(优化版)

uniapp项目需要把微信小程序原生语法的功能代码嵌套过来,无需把原生代码转换为uniapp,可以配置拷贝的方式集成过来 1、拷贝代码包到src目录 2、vue.config.js中配置原生代码包直接拷贝到编译目录中 3、pages.json中配置分包目录,原生入口组件的路径 4、manifest.json中配置分包,使用原生组件 5、需要把原生代码包里的页面修改成组件的方

公共筛选组件(二次封装antd)支持代码提示

如果项目是基于antd组件库为基础搭建,可使用此公共筛选组件 使用到的库 npm i antdnpm i lodash-esnpm i @types/lodash-es -D /components/CommonSearch index.tsx import React from 'react';import { Button, Card, Form } from 'antd'

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM

通过SSH隧道实现通过远程服务器上外网

搭建隧道 autossh -M 0 -f -D 1080 -C -N user1@remotehost##验证隧道是否生效,查看1080端口是否启动netstat -tuln | grep 1080## 测试ssh 隧道是否生效curl -x socks5h://127.0.0.1:1080 -I http://www.github.com 将autossh 设置为服务,隧道开机启动

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std

Eclipse+ADT与Android Studio开发的区别

下文的EA指Eclipse+ADT,AS就是指Android Studio。 就编写界面布局来说AS可以边开发边预览(所见即所得,以及多个屏幕预览),这个优势比较大。AS运行时占的内存比EA的要小。AS创建项目时要创建gradle项目框架,so,创建项目时AS比较慢。android studio基于gradle构建项目,你无法同时集中管理和维护多个项目的源码,而eclipse ADT可以同时打开

android 免费短信验证功能

没有太复杂的使用的话,功能实现比较简单粗暴。 在www.mob.com网站中可以申请使用免费短信验证功能。 步骤: 1.注册登录。 2.选择“短信验证码SDK” 3.下载对应的sdk包,我这是选studio的。 4.从头像那进入后台并创建短信验证应用,获取到key跟secret 5.根据技术文档操作(initSDK方法写在setContentView上面) 6.关键:在有用到的Mo