1,Android代码设置Shape,corners,Gradient  (http://blog.csdn.net/houshunwei/article/details/17392409)

int strokeWidth = 5; // 3dp 边框宽度
int roundRadius = 15; // 8dp 圆角半径
int strokeColor = Color.parseColor("#2E3135");//边框颜色
int fillColor = Color.parseColor("#DFDFE0");//内部填充颜色 GradientDrawable gd = new GradientDrawable();//创建drawable
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);
setBackgroundDrawable(gd);
但是如果想设置Gradient的渐变色该咋办呢?
方法是改变GradientDrawable的创建方法:
int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分别为开始颜色,中间夜色,结束颜色

GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
参考:
http://stackoverflow.com/questions/17667964/how-to-create-shape-with-solid-corner-stroke-in-java-code
http://stackoverflow.com/questions/4177401/gradientdrawable-in-code
http://www.iteye.com/topic/1117635

2,,checkbox 代码设计
//取得CheckBox对象
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
 
//取得设置好的drawable对象
Drawable drawable = this.getResources().getDrawable(R.drawable.checkbox_style);
 
//设置drawable对象的大小
drawable.setBounds(0,0,40,40);
 
//设置CheckBox对象的位置,对应为左、上、右、下
checkBox.setCompoundDrawables(drawable,null,null,null);

3, Android 动态创建Drawable selector

http://blog.csdn.net/csm_qz/article/details/48520925

创建selector有两种方法,一种是定义xml文件,一种是创建StateListDrawable对象,完全可以用创建StateListDrawable来代替xml,它的好处是可以在程序运行时动态的调整背景颜色或者背景图片。

一.xml创建selector方法如下: 
定义一个switch_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/switch_bg_disabled_emui" android:state_enabled="false"/>
<item android:drawable="@drawable/switch_bg_on_emui" android:state_pressed="true"/>
<item android:drawable="@drawable/switch_bg_on_emui" android:state_focused="true"/>
<item android:drawable="@drawable/switch_bg_on_emui" android:state_checked="true"/>
<item android:drawable="@drawable/switch_bg_off_emui"/>
</selector>

在Activity按下面方法使用

Drawable drawable = getResourse().getDrawable(R.drawable.switch_selector);
ImageView iv = new ImageView(this);
iv.setBackground(drawable);

二.用StateListDrawable来代替xml创建selector:

        private StateListDrawable createDrawableSelector(Context context)
{
Drawable checked = context.getResources().getDrawable(R.drawable.switch_bg_on_emui);
Drawable unchecked = context.getResources().getDrawable(R.drawable.switch_bg_off_emui);
Drawable disabled = context.getResources().getDrawable(R.drawable.switch_bg_disabled_emui);
StateListDrawable stateList = new StateListDrawable();
int statePressed = android.R.attr.state_pressed;
int stateChecked = android.R.attr.state_checked;
int stateFocused = android.R.attr.state_focused;
int stateensable = android.R.attr.state_enabled;
stateList.addState(new int[] {-stateensable}, disabled);
stateList.addState(new int[] {stateChecked}, checked);
stateList.addState(new int[] {statePressed}, checked);
stateList.addState(new int[] {stateFocused}, checked);
stateList.addState(new int[] {}, unchecked);
return stateList;
}

其中stateList.addState()表示一个状态对应一个Drawable,在Activity里面按下面方法使用

Drawable drawable = createDrawableSelector(this);
ImageView iv = new ImageView(this);
iv.setBackground(drawable); 4,
动态获取R.drawable.xx资源
String imageName = "index_fragmen"+getColor();
final int resId = context.getResources().getIdentifier(imageName, "drawable", context.getPackageName());
if (resId != 0) {
Log.e("mytest", "可以获取到");
holder.btnCoupon.setBackgroundResource(resId);
}
												

最新文章

  1. go语言的print
  2. Java性能优化权威指南-读书笔记(一)-操作系统性能监控工具
  3. linux与windows的文本文件之间的转换
  4. Count and Say [LeetCode 38]
  5. Android下各个按键对应的key code
  6. poj 1026(置换群)
  7. Linux学习笔记31——网络信息
  8. spring boot之使用springfox swagger展示restful的api doc
  9. TFboy养成记 CNN
  10. jackson xml转对象 对象转xml
  11. java代码之美(9)---guava之Lists、Maps
  12. MR单元测试以及DeBug调试
  13. Linux 搭建Hadoop集群 成功
  14. 从零开始学 Web 之 jQuery(七)事件冒泡,事件参数对象,链式编程原理
  15. 扩展 StackExchange.Redis 支持实体
  16. Python基础教程-第3版(文字版) 超清文字-非扫描版 [免积分、免登录]
  17. Ext面板
  18. maven-surefire-plugin的forkMode分析
  19. unity 查看打包资源占用
  20. 【MFC】将当前的日期转化为1970年开始的秒计数

热门文章

  1. 多进程的基本使用--multiprocessing 【转】
  2. 2 Model层 -定义模型
  3. Java-读取txt生成excel
  4. 在Foxmail中添加阿里云企业邮箱账号
  5. gulp基本入门
  6. OpenStack之虚机冷迁移代码简析
  7. IOS开发学习笔记010-面向对象的三大特性
  8. uReplicator实现分析
  9. python-day4-装饰器的使用
  10. day05_08 列表讲解、切片、内置方法