我需要你帮助我完成一个任务,我一直在尝试制作一个圆角按钮并仅显示其边框,我需要能够根据从Web服务获取的信息在程序中改变它的颜色。到目前为止,我已经尝试使用drawable添加形状,这样就可以得到具有边框颜色的圆角形状,但是我不知道是否可能更改它的颜色,因为它是默认添加到drawable中的。
<?xml version="1.0" encoding="UTF-8"?>
<stroke android:width="3dp"
android:color="#ff000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
这是我使用的可绘制对象,然后我尝试添加形状,为按钮创建一个自定义类并更改onDraw方法,但我得到的形状有点奇怪。
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(strokeColor);
paint.setStrokeWidth(5.0f);
int h = this.getHeight();
int w = this.getWidth();
//final RectF rect = new RectF();
RectF oval1 = new RectF(0, 0, w, h);
canvas.drawRoundRect(oval1, 40, 40, paint);
}
除了奇怪的形状之外,我还使用“setText”方法动态添加文本,但它没有显示出来,它可以获取描边的颜色,但无法显示文本。
buttonCTA = ButterKnife.findById(this, R.id.btnCTA);
buttonCTA.setTextColor(Color.parseColor(valueColor));
buttonCTA.setStrokeColor(valueColor);
buttonCTA.setText("test");