java - 如何以编程方式更改主屏幕小部件的形状颜色
问题描述
我希望我的主屏幕小部件是一个带角的矩形,所以我有一个可绘制的形状。然而,纯色需要在按下按钮时更改以匹配我的文本背景颜色。我可以通过这种方式更改我的文本背景颜色:
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
backgroundColor = colors[mCounter];
views.setInt(R.id.appwidget_text, "setBackgroundColor", backgroundColor);
我尝试了以下但没有运气:
views.setInt(R.drawable.layout_bg, "setBackgroundColor", backgroundColor);
我还尝试更改我的小部件的 backgroundTint,以及以下内容:
views.setInt(R.id.appwidget_layout, "setBackgroundTint", backgroundColor);
这也没有任何作用。
有解决办法吗?
解决方案
int[] color = new int[2];
color[0] = Color.parseColor("#000000");
color[1] = Color.parseColor("#000000");
View view = new RelativeLayout(this);
GradientDrawable drawable = (GradientDrawable) view.getBackground();
drawable.setColors(color);
尝试这个。对于你的问题,
int[] color = new int[2];
color[0] = Color.parseColor("#000000");
color[1] = Color.parseColor("#000000");
GradientDrawable drawable = (GradientDrawable) views.getBackground();
drawable.setColors(color);
希望它会有所帮助。:)
推荐阅读
- python - VSCode 默认 Shell 指向错误的 python 环境
- hololens - 添加复杂的点击事件 HoloLens
- c - 如何为c中堆上声明的变量赋值?
- android - Android Studio 4.1 gradle 6.7:获取 manifestOutputDirectory 错误
- python-3.x - 在指定位置的整数列中添加逗号(熊猫)
- python - spyder中的内核重新启动(Ctrl +。)不起作用
- salesforce - 在 Mulesoft Api 开发中,要将任何应用程序与 Salesforce 集成,我们是否需要使用 Salesforce 连接器或 http 请求?
- javascript - API 结果说它是空的,但它不是
- node.js - 无法通过 id 找到文档
- webdriver-io - 在调用 browser.getTabIds() 时获取“browser.getTabIds”不是 webdriverio 中的函数