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);
}