如何将可绘制对象分配给小部件按钮 [英] How to assign a drawable to a widgets button

查看:70
本文介绍了如何将可绘制对象分配给小部件按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以提供帮助.不知道这是否有可能,但是可以.

hoping someone can help. Not sure if this is even possible but here goes.

我有一个包含单个按钮的Android小部件.我想以编程方式将drawable设置为背景.例如,在标准活动中,我可能会做...

I have an Android widget which consists of a single button. I want to programmatically set a drawable as the background. For example in a standard activity I might do...

Button mybutton = (Button) findViewById(R.id.abutton);
GradientDrawable gd = new GradientDrawable();
gd.setCornerRadius(45);
gd.setColor(Color.parseColor("#FFFFFF));
mybutton.setBackground(gd);

我要这样做的原因是,以编程方式将小部件按钮的颜色更改为用户选择的颜色,同时还要保持按钮的圆角,如上面的可绘制对象所定义.

The reason I am trying to do this is to programmatically change the colour of the widget button to a colour the user picks but also maintain the rounded corners of the button as defined in the drawable above.

我已经确定可以像这样更改小部件按钮的颜色.

I've worked out I can change the colour of the widget button like so.

RemoteViews views;
views = new RemoteViews(context.getPackageName(), R.layout.mylayout);
views.setInt(R.id.widgetbutton,"setBackgroundColor", Color.parseColor(widgethexcolor));

但是这会将按钮重置为默认的矩形形状,而没有圆角,因此我希望将其设置为可绘制的.

But this resets the button to its default rectangular shape without the rounded corners so I would prefer to make it a drawable.

有什么想法吗?

谢谢!

自然

推荐答案

您可以通过调用

You could change the color by calling Drawable.setColorFilter(int color, PorterDuff.Mode mode) or Drawable.setColorFilter(ColorFilter colorFilter). I think this approach will prevent you from losing the rounded corners.

这篇关于如何将可绘制对象分配给小部件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆