以编程方式更改小部件的渐变背景 [英] Programmatically Change Gradient Background of Widget
本文介绍了以编程方式更改小部件的渐变背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要完成的工作:
int[] colors = new int[]{colorDark,colorLight}
GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
remoteView.setBackgroundDrawable(gd); //method does not exist
显然这是不可能的.
我该怎么做?(如果可能的话)
我不想在xml文件中为不同的颜色创建多个形状,因为这会限制选项.
I do not want to have to create multiple shapes in xml files for different colors, because this limits options.
我尝试将可绘制对象转换为位图并调用setImageViewBitmap
.我使用此代码进行了转换,并使用了此代码以获取宽度/高度,但是我无法填充小部件(此外,设备的显示宽度/高度确实不是我所需要的)
I have tried converting my drawable to a bitmap and calling setImageViewBitmap
. I converted with this code and used this code to get the width/height, but I'm unable to get the widget to be filled (additionally, the device's display width/height really aren't what I need anyway)
推荐答案
我只是在猜测.您可以尝试扩展RemoteViews并覆盖apply函数吗?
I am just guessing. Can you try to extend RemoteViews and override the apply function:
public class MySpecialRemoteViews extends RemoteViews {
//add the Constructors
public View apply(Context context, ViewGroup parent) {
View result = super.apply(context, parent);
//your code
int[] colors = new int[]{colorDark,colorLight}
GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
result.setBackgroundDrawable(gd);
//end of your code
return result;
}
}
这篇关于以编程方式更改小部件的渐变背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文