Java代码更改shape和selector文件的颜色值
Android里面经常会使用shape或者selector来定制一些View的背景。那么如果想要动态更改shape或者seletor文件中的颜色值,该怎么处理呢?
一、Java代码更改shape的颜色值
shape文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- popwindow样式文本框区域背景 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> <!-- 圆角 -->
<corners
android:radius="5dp"/> <!-- 描边 -->
<stroke
android:width="1dp"
android:color="#709e9e9e"/> <!-- 内边距 -->
<!-- <padding
android:left="5dp"
android:right="5dp"
android:top="5dp"
android:bottom="10dp"/> --> <!-- 填充 -->
<solid android:color="#ffffffff" /> </shape>
Java代码如下:
GradientDrawable myGrad = (GradientDrawable)view.getBackground();
myGrad.setColor(ContextCompat.getColor(mContext,R.color.spinnerpop_notedit_bg_color)); myGrad.setStroke(2,ContextCompat.getColor(mContext,R.color.spinnerpop_notedit_bg_color));
二、Java代码更改selector的颜色值
selector文件如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- popwindow样式下拉菜单列表项背景 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 触发时、按压时:上浅颜色->下深颜色 -->
<!-- android:angle="270"自上而下
android:angle="90"自下而上 -->
<item android:state_focused="true">
<shape>
<!-- 填充-->
<solid android:color="#709e9e9e"/>
<!-- 描边 -->
<!-- <stroke
android:width="1dp"
android:color="#709e9e9e"/> -->
<!-- 圆角 -->
<corners
android:radius="5dp" />
</shape>
</item> <item android:state_pressed="true">
<shape>
<!-- 填充-->
<solid android:color="#709e9e9e"/>
<!-- 描边 -->
<!-- <stroke
android:width="1dp"
android:color="#709e9e9e"/> -->
<!-- 圆角 -->
<corners
android:radius="5dp" />
</shape>
</item> <!-- 正常时:透明色 -->
<item>
<shape>
<!-- 填充-->
<solid android:color="@android:color/transparent"/>
<!-- 圆角 -->
<!--<corners
android:radius="5dp" />-->
</shape>
</item> </selector>
java代码如下:
因为StatelistDrawable内获取状态以及drawable的方法都是被隐藏的,所以只有利用Java的反射机制来获取各个状态,以及各个状态对应的drawable。
StateListDrawable mySelectorGrad = (StateListDrawable)view.getBackground();
try {
Class slDraClass = StateListDrawable.class;
Method getStateCountMethod = slDraClass.getDeclaredMethod("getStateCount", new Class[0]);
Method getStateSetMethod = slDraClass.getDeclaredMethod("getStateSet", int.class);
Method getDrawableMethod = slDraClass.getDeclaredMethod("getStateDrawable", int.class);
int count = (Integer) getStateCountMethod.invoke(mySelectorGrad, new Object[]{});//对应item标签
Log.d(TAG, "state count ="+count);
for(int i=0;i < count;i++) {
int[] stateSet = (int[]) getStateSetMethod.invoke(mySelectorGrad, i);//对应item标签中的 android:state_xxxx
if (stateSet == null || stateSet.length == 0) {
Log.d(TAG, "state is null");
GradientDrawable drawable = (GradientDrawable) getDrawableMethod.invoke(mySelectorGrad, i);//这就是你要获得的Enabled为false时候的drawable
drawable.setColor(Color.parseColor(checkColor));
} else {
for (int j = 0; j < stateSet.length; j++) {
Log.d(TAG, "state =" + stateSet[j]);
Drawable drawable = (Drawable) getDrawableMethod.invoke(mySelectorGrad, i);//这就是你要获得的Enabled为false时候的drawable
}
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
android:state_focused 对应的Java中的整型值 : android.R.attr.state_focused,如果为false,则对应 - android.R.attr.state_focused【“-”负号表示对应的属性值为false】
android:state_pressed 对应的Java中的整型值 : android.R.attr.state_pressed,如果为false,则对应 - android.R.attr.state_pressed【“-”负号表示对应的属性值为false】
参考资料
获取xml里设置的statelistdrawable内的各个状态对应的drawable
Java代码更改shape和selector文件的颜色值的更多相关文章
- 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有不爽.此篇博客教你如何方便快捷地把java代码,打包成jar文件以 ...
- 使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置
1.使用java代码动态配置与xml文件结合的方式使用mybatis-generator生成代码配置 2.上代码:在resources目录下新建:generatorConfiguration.xml文 ...
- 代码设置Shape和Selector
开发中经常需要使用Shape和Selector,如果每个都用xml设置的话,会占用apk大小,同时命名多了也会混乱,使用代码来设置会方便很多. 需要用到2个类:GradientDrawable和Sta ...
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- java代码实现ftp服务器的文件上传和下载
java代码实现文件上传到ftp服务器: 1:ftp服务器安装: 2:ftp服务器的配置: 启动成功: 2:客户端:代码实现文件的上传与下载: 1:依赖jar包: 2:sftpTools 工具类: ...
- 用java代码解决excel打开csv文件乱码问题
Java 读取csv文件后,再保存到磁盘上,然后直接用Excel打开,你会发现里面都是乱码. 贴上代码: public class Test { public static void main(S ...
- java代码用dom4j解析xml文件的简单操作
时间: 2016/02/17 目标:为telenor的ALU Femto接口写一个采集xml文件并解析出locationName标签里的值,然后更新到数据库中. 从网上搜了下,有四种常用的解析xml的 ...
- 在java代码中用xslt处理xml文件
java处理xml文件 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExceptio ...
- Java代码中解压RAR文件
import java.io.File; import java.io.FileOutputStream; import de.innosystec.unrar.Archive; import de. ...
随机推荐
- Jenkins入门,介绍、安装
一.介绍 Jenkins: 三.安装 Jenkins: 第一种方式: 下载 windows 版本:进入 Jenkins 官网:https://jenkins.io/ 进入下 ...
- mysql sql mode
/usr/local/mysql/bin/mysqld --verbose --help | grep -A 1 'Default options' (1)关于配置文件路径 有时候,我发现虽然尝试修改 ...
- XBee模块户外通信距离测试
Digi的XBee模块在市面上同类产品中,除了稳定性和可靠性最受推崇外,在距离测试中一般都比竞争对手的产品略胜一筹.户外测试需要注意避免模块自身以外的因素造成的测试结果不理想. 2.4G的模块有Zig ...
- Mysql双主互备+keeplived高可用架构介绍
一.Mysql双主互备+keeplived高可用架构介绍 Mysql主从复制架构可以在很大程度保证Mysql的高可用,在一主多从的架构中还可以利用读写分离将读操作分配到从库中,减轻主库压力.但是在这种 ...
- js浮点数加减乘除精度不准确
做个记录,以备不时之需 //加法 Number.prototype.add = function(arg){ var r1,r2,m; try{r1=this.toString().split(&qu ...
- uboot常用的函数
http://blog.csdn.net/ooonebook/article/details/53206623 以下例子都以project X项目tiny210(s5pv210平台,armv7架构)为 ...
- MySQL数据库(三)索引总结
一.什么是索引? 索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存. 如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录 ...
- [leetcode268]Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- vim配置文件.vimrc
20171127备份 syntax on "自动语法高亮 set number "显示行号 set autoindent "回车后自动缩进 set tabstop=4 & ...
- cisco基本配置命令
实验命令 router> enable 从用户模式进入特权模式 router# disable or exit 从特权模式退出到用户模式 router# show sessions 查看本机上的 ...