如何在小部件中以编程方式设置android:backgroundTint?

3

我有一个小部件布局:

<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属性可以使用我提供的特定颜色来着色背景。

问题是,我想在adroid小部件中使用RemoteViews类以编程方式设置任何颜色。

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属性的方法(例如View类的setBackgroundTintList方法或其他方法)。我该如何解决这个问题?

2
你解决了这个问题吗? - lyncx
暂时没有解决方案 :( - Vitaly
1个回答

0

2
请你能否澄清一下:我如何使用这些“新”的方法以编程方式设置android:backgroundTint="@color/md_blue_500"的值?(同时保存android:background="@drawable/widget_border_black"的值) - Vitaly

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接