public class ColorTextView extends TextView {
private int OvalDp;
public ColorTextView(Context context, AttributeSet attrs) {
super(context, attrs);
OvalDp= DensityUtils.dp2px(5,context);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.ColorTextView);
ShapeDrawable shapeDrawable=new ShapeDrawable();
shapeDrawable.setShape(new RoundRectShape(new float[]{OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp},null,null));
shapeDrawable.getPaint().setColor(typedArray.getColor(R.styleable.ColorTextView_colorBackground, Color.BLACK));
ShapeDrawable shapeDrawablePre=new ShapeDrawable();
shapeDrawablePre.setShape(new RoundRectShape(new float[]{OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp},null,null));
shapeDrawablePre.getPaint().setColor(typedArray.getColor(R.styleable.ColorTextView_colorBackgroundPre, Color.BLACK));
StateListDrawable stateListDrawable=new StateListDrawable();
stateListDrawable.addState(new int[]{-android.R.attr.state_focused,-android.R.attr.state_pressed},shapeDrawable);
stateListDrawable.addState(new int[]{},shapeDrawablePre);
setBackgroundDrawable(stateListDrawable);
setTextColor(typedArray.getColor(R.styleable.ColorTextView_colorTextColor,Color.BLACK));
setGravity(Gravity.CENTER);
setTextSize(20);
setPadding(0,DensityUtils.dp2px(8,context),0,DensityUtils.dp2px(8,context));
}
}
内容有用到的dp转px工具
/**
* dp转px
*/
public static int dp2px(float dpVal, Context context) {
try {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources()
.getDisplayMetrics());
} catch (Exception ex) {
BananasLoger.debug(ex.getMessage());
return 0;
}
}
private int OvalDp;
public ColorTextView(Context context, AttributeSet attrs) {
super(context, attrs);
OvalDp= DensityUtils.dp2px(5,context);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.ColorTextView);
ShapeDrawable shapeDrawable=new ShapeDrawable();
shapeDrawable.setShape(new RoundRectShape(new float[]{OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp},null,null));
shapeDrawable.getPaint().setColor(typedArray.getColor(R.styleable.ColorTextView_colorBackground, Color.BLACK));
ShapeDrawable shapeDrawablePre=new ShapeDrawable();
shapeDrawablePre.setShape(new RoundRectShape(new float[]{OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp,OvalDp},null,null));
shapeDrawablePre.getPaint().setColor(typedArray.getColor(R.styleable.ColorTextView_colorBackgroundPre, Color.BLACK));
StateListDrawable stateListDrawable=new StateListDrawable();
stateListDrawable.addState(new int[]{-android.R.attr.state_focused,-android.R.attr.state_pressed},shapeDrawable);
stateListDrawable.addState(new int[]{},shapeDrawablePre);
setBackgroundDrawable(stateListDrawable);
setTextColor(typedArray.getColor(R.styleable.ColorTextView_colorTextColor,Color.BLACK));
setGravity(Gravity.CENTER);
setTextSize(20);
setPadding(0,DensityUtils.dp2px(8,context),0,DensityUtils.dp2px(8,context));
}
}
内容有用到的dp转px工具
/**
* dp转px
*/
public static int dp2px(float dpVal, Context context) {
try {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, dpVal, context.getResources()
.getDisplayMetrics());
} catch (Exception ex) {
BananasLoger.debug(ex.getMessage());
return 0;
}
}