[英]How to highlight a button in Android with java code?
我四处搜索并找到了以下代码,但它没有做我想要的:
private class ButtonListener implements View.OnClickListener
{
public void onClick(View v)
{
for (int index = 0; index < GATE.col_count + 1; index++)
if (gateView.isPinButton((Button) v, index))
{
gate.Current_Id = index;
// ((Button) v).setBackgroundColor(Color.rgb(198,68,18));
ShapeDrawable shapedrawable = new ShapeDrawable();
shapedrawable.setShape(new RectShape());
shapedrawable.getPaint().setColor(Color.rgb(88,236,236));
shapedrawable.getPaint().setStrokeWidth(10f);
shapedrawable.getPaint().setStyle(Paint.Style.STROKE);
// shapedrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
// ((Button) v).setBackgroundColor(Color.parseColor("#66FF0000"));
((Button) v).setBackground(shapedrawable);
((Button) v).getBackground().setColorFilter(0x37000000, PorterDuff.Mode.SRC_ATOP);
v.invalidate();
return;
}
}
}
单击按钮之前,该按钮如下所示:
点击后是这样的:
但我想要的是看起来像这样,所以按钮的中心仍然像以前一样灰色,但边框是高亮的:
那么从java代码中正确的方法是什么?
这不是最好的解决方案,但作为选项,您可以将按钮放在某个容器中,例如在 FrameLayout 中,将容器的宽度和高度设置为 wrap_content,将背景颜色设置为透明或 @null 并将按钮置于其中心,接下来,您需要为容器设置一些填充,以便容器在按钮后面可见,最后当您单击按钮时,只需在按钮的 onClickListener 中更改容器的背景颜色
我找到了答案,就像这样:
GradientDrawable drawable = new GradientDrawable();
drawable.setShape(GradientDrawable.RECTANGLE);
drawable.setStroke(5, Color.rgb(66,218,236));
drawable.setColor(Color.rgb(214,215,215));
((Button) v).setBackground(drawable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.