用代码画shape背景

1. 首先上一个效果图


1


2.上代码

GradientDrawable drawable =newGradientDrawable();

drawable.setCornerRadius(20);

drawable.setColor(Color.parseColor("#f00000"));

drawable.setStroke(5,Color.parseColor("#f0f000"));

tv_123.setBackgroundDrawable(drawable);


3. 代码添加选择器

StateListDrawableselectDrawable =newStateListDrawable();

selectDrawable.addState(new int[]{-android.R.attr.state_pressed},drawable1);

selectDrawable.addState(new int[]{android.R.attr.state_pressed},drawable);

你可能感兴趣的:(用代码画shape背景)