草庐IT

Android AppWidget TextView : How to set background color at run time

coder 2023-06-07 原文

我正在尝试创建一个 AppWidget,其中 TextView 的背景颜色以指定的周期间隔随机变化。

TextView 在布局 xml 文件中定义为

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TextView  
        android:id="@+id/message"
        android:background="#ff99ff"
        android:text="Hello Widget" />
</LinearLayout>

在更新方法中,我已将布局加载为

RemoteViews remoteView=new RemoteViews(context.getPackageName(),R.layout.widget_message);

要更改 TextView 的背景,我使用了以下语句

remoteView.setInt(R.id.message, "setBackgroundResource", R.color.col_1);

但我得到一个小部件,说加载小部件有问题。如果我删除上述行一切正常。

LogCat 说:

updateAppWidget couldn't find any view, using error view

android.widget.RemoteViews$ActionException: view: android.widget.TextView can't use method with RemoteViews: setBackgroundResource(int)

最佳答案

试试这个,它会很好用。

remoteView.setInt(R.id.message, "setBackgroundColor", 
        android.graphics.Color.BLACK);

关于Android AppWidget TextView : How to set background color at run time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2453543/

有关Android AppWidget TextView : How to set background color at run time的更多相关文章

随机推荐