修改Switch 的颜色
1:效果图

2:布局
<Switch
android:id="@+id/switch_bg"
style="@style/switchStyle"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:checked="false"
android:switchMinWidth="52dp" />
3:switchStyle
<style name="switchStyle">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:textOff">""</item>
<item name="android:textOn">""</item>
<item name="android:thumb">@drawable/switch_thumb</item>
<item name="android:thumbTextPadding">10dp</item>
<item name="android:track">@drawable/switch_track</item>
</style>
4: switch_thumb
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/abc_btn_switch_to_on_mtrl_00012" />
<item android:drawable="@drawable/abc_btn_switch_to_on_mtrl_00001" />
</selector>
5: switch_track
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="@dimen/switch_w"
android:height="@dimen/switch_h"/>
<solid android:color="@color/colorDefaultThemeGreen"/>
<corners android:radius="@dimen/switch_r"/>
</shape>
</item> <item android:state_checked="false" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="@dimen/switch_w"
android:height="@dimen/switch_h"/>
<solid android:color="@color/colorGray"/>
<corners android:radius="@dimen/switch_r"/>
</shape>
</item> </selector>
6:实现方法
private void setSwitchColor(Switch switch_bg,int color){
StateListDrawable stateListDrawable = (StateListDrawable) switch_bg.getTrackDrawable();
try {
Class stateListDrawableClass = StateListDrawable.class;
Method getStateCountMethod = stateListDrawableClass.getDeclaredMethod("getStateCount", new Class[0]);
Method getStateSetMethod = stateListDrawableClass.getDeclaredMethod("getStateSet", int.class);
Method getDrawableMethod = stateListDrawableClass.getDeclaredMethod("getStateDrawable", int.class);
int count = (Integer) getStateCountMethod.invoke(stateListDrawable, new Object[]{});
Log.d("Tag:", "setSwitchColor: --count: "+count);
for (int i = 0; i < count; i++) {
int[] stateSet = (int[]) getStateSetMethod.invoke(stateListDrawable, 0);
if (stateSet == null || stateSet.length == 0) {
GradientDrawable drawable = (GradientDrawable) getDrawableMethod.invoke(stateListDrawable, i);
drawable.setColor(color);
} else {
Log.d("Tag:", "setSwitchColor:length: "+stateSet.length);
for (int j = 0; j < stateSet.length; j++) {
Log.d("Tag:", "setSwitchColor: "+stateSet[j]);
if(stateSet[j]==android.R.attr.state_checked){
Drawable drawable = (Drawable) getDrawableMethod.invoke(stateListDrawable, j);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
}else {
Drawable drawable = (Drawable) getDrawableMethod.invoke(stateListDrawable, j);
drawable.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
7:code
链接: https://pan.baidu.com/s/1h-KzOa-_mOrqlsSMqttZKQ 密码: i9uh
修改Switch 的颜色的更多相关文章
- Android Studio -修改LogCat的颜色
Android Studio -修改LogCat的颜色 author:Kang,Leo weibo:http://weibo.com/kangyi 效果图 设置 Preference->Edit ...
- 复选框输入Android Studio 如果修改LogCat的颜色,默认全是黑色看着挺不舒服的
今天一直在查找复选框输入之类的问题,上午正好有机会和大家分享一下. 怎么找到并表现LogCat这里就不需要再讲了吧,主要说一下本篇的主题,如何修改他的颜色 .我们在使用Eclipse的时候应该都用过L ...
- 修改tabbar 字体颜色
NSDictionary *seletedTextAttrs = @{NSForegroundColorAttributeName:[UIColor orangeColor]}; 修改tabbar 字 ...
- 如何解决在GDI画图中,多次修改画笔的颜色
首先创建个画笔对象: CPen gPen;gPen.CreatePen(PS_SOLID, 1, RGB(120,120,130));//一定灰度的画笔〈/br〉CPen* pOldPen = pDC ...
- 修改UISearchBar背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性.方法一:是直接将 UISearc ...
- jquery入门 修改网页背景颜色
我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> & ...
- 修改cocos2dx 背景颜色
只需要在AppDelegate的设置FPS后面加入一行: glClearColor(1.0, 1.0, 1.0, 1.0); 同理如果要修改成其它颜色,只需修改里面的值即可( r, g, b, a);
- ubuntu修改顶栏颜色
title: ubuntu修改顶栏颜色 toc: false date: 2018-09-29 19:14:01 categories: methods tags: Ubuntu 编辑shell主题的 ...
- HTML 颜色输入框修改事件的触发,以及获取修改后的颜色
HTML 颜色输入框修改事件的触发,以及获取修改后的颜色 <!DOCTYPE html> <html lang="en"> <head> < ...
随机推荐
- 一、Scrapy入门教程
本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/tutorial.html 在本篇教程中,我们假定您已经安装好Scrapy ...
- Django REST framework - 视图
目录 Django REST framework 视图GenericAPIView GenericAPIView 例子 属性 混入 具体视图类 自定义基类 Django REST framework ...
- String formatting in Python
| \n | 换行 || \t | 制表符 || \ | 转义 || \\ | \ | the '%' operator is used to format a set of va ...
- PAT 1057. Stack
Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...
- 【codeforces 801D】Volatile Kite
[题目链接]:http://codeforces.com/contest/801/problem/D [题意] 给你一个凸多边形的n个点; 然后允许你将每个点移动到距离不超过D的范围内; 要求无论如何 ...
- noip模拟赛 钻石
分析:用裸暴力可以得60分,每次dfs,看第i个盒子到底有没有钻石就行了.其实这很像0/1背包问题,只是多了一个m的限制.这要怎么办呢?因为概率是可以加减的,所以可以先不考虑m的限制,求出概率,然后d ...
- [cf 599D] Spongebob and Squares
据题意: $K=\sum\limits_{i=0}^{n-1}(n-i)*(m-i)$ $K=n^2m-(n+m)\sum{i}+\sum{i^2}$ 展开化简 $m=(6k-n+n^3)/(3n^2 ...
- nyoj_85_有趣的数_201312122130
有趣的数 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 把分数按下面的办法排成一个数表. 1/1 1/2 1/3 1/4..... 2/1 2/2 ...
- D - Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- systemtap dtrace与 oracle
https://fritshoogland.wordpress.com/page/3/ http://externaltable.blogspot.com/2013/06/dtrace-explora ...