我正在编辑它以解释我试图创建一个按钮作为带有圆角的小部件的一部分,可以通过编程将其颜色更改为任何颜色。当我当前使用 SetInt 执行此操作时,按钮的默认形状变回矩形。 RemoteViews 似乎不支持在 Activity 中实现此目的的常用方法,因此这个问题可能无法回答。无论如何,感谢 Mike 指出这一点。
我想知道这是否很简单。我想创建一个新的按钮类——它基本上只是一个带圆角的普通按钮。这样做的原因是我希望能够以编程方式将按钮的背景颜色更改为任何使用......
mybutton.setBackgroundColor(Color.parsecolor(somehexvalue));
按钮不会失去其形状(即恢复为矩形)。
我已经创建了我的按钮类,并且明白我需要覆盖 OnDraw 方法,但我并不真正理解此时我是如何应用自定义形状的。就这么简单吗?
@RemoteView
public class custombutton extends android.support.v7.widget.AppCompatButton {
Paint paint = null;
public custombutton(Context context) {
super(context);
paint = new Paint();
}
public custombutton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public custombutton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
What do I need to do here to create a button with rounded corners???
}
}
谢谢!!!!
最佳答案
为此您不需要 Customview。
代替 setBackgroundColor,在需要更改时检索可绘制的背景 背景,并设置其颜色:
v.setBackgroundResource(R.drawable.tags_rounded_corners);
GradientDrawable drawable = (GradientDrawable) v.getBackground();
if (i % 2 == 0) {
drawable.setColor(Color.RED);
} else {
drawable.setColor(Color.BLUE);
}
关于java - 如何创建自定义按钮形状类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54138736/