首页 > 解决方案 > 如何在 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());

它允许我拥有小部件背景颜色,但没有我的可绘制对象(边框)。

两种方法 (setBackgroundResourcesetBackgroundColor) 不能同时工作!小部件中的时间:(

所以,我想以编程方式设置:android:backgroundTint属性值(并提供而不是蓝色 - 任何!!!颜色值 - 不仅仅是一个,来自资源 xml)

android:backgroundTint="@color/md_blue_500"

问题是 RemoteViews 类没有设置 backgroundTint 属性的方法(例如:setBackgroundTintListView 类的方法或其他)我该如何解决我的问题?

标签: androidwidget

解决方案


推荐阅读