android - 如何在 Widget 中以编程方式设置 android:backgroundTint
问题描述
我有下一个小部件布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_view"
android:backgroundTint="@color/md_blue_500"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/widget_border_black">
</RelativeLayout>
我有这样的drawable(widget_border_black.xml):
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/dark_gray"/>
<solid
android:color="#000000"/>
<corners
android:radius="10dp"/>
</shape>
所以drawable为我画了一些边框(等等)。我提供了具有特定颜色的 android:backgroundTint 属性颜色背景。
问题是我想使用 RemoteViews 类在 android 小部件中以编程方式设置任何颜色
RemoteViews views;
我可以设置:
views.setInt(R.id.my_view, "setBackgroundResource", R.drawable.widget_border_black);
它允许我有带边框的小部件,但没有我特定的背景颜色我可以设置:
views.setInt(R.id.my_view, "setBackgroundColor", note.getColor());
它允许我拥有小部件背景颜色,但没有我的可绘制对象(边框)。
两种方法 (setBackgroundResource
和setBackgroundColor
) 不能同时工作!小部件中的时间:(
所以,我想以编程方式设置:android:backgroundTint
属性值(并提供而不是蓝色 - 任何!!!颜色值 - 不仅仅是一个,来自资源 xml)
android:backgroundTint="@color/md_blue_500"
问题是 RemoteViews 类没有设置 backgroundTint 属性的方法(例如:setBackgroundTintList
View 类的方法或其他)我该如何解决我的问题?
解决方案
推荐阅读
- java - 将数组移动到不同的类
- python - 在列表理解中编写长 if 语句的 Pythonic 方法
- python - 如何访问 NetworkX 中节点的第一个邻居?
- javascript - 在事件监听器中执行函数失败
- python - 将变量传递给表单操作
- python - 循环遍历多个 txt 文件并计算 Python 中所选单词的频率
- python - Python Dash - 来自另一个文件的回调值
- rest - Spring REST 应用程序中的存储库
- python - 如何随机获取 numpy 数组的一定数量的元素,每个类至少有一个元素?
- python - 在 Pandas 中有效地合并两个数据框的列和行