我正在尝试创建一个 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);
但我收到一个小部件,说加载小部件时出现问题。如果我删除上面的行,一切都会正常。
日志猫 说:
updateAppWidget 找不到任何
视图,使用错误视图
android.widget.RemoteViews$ActionException:
视图:android.widget.TextView 不能
使用 RemoteViews 的方法:
设置背景资源(int)
试试这个,它会工作得很好。
remoteView.setInt(R.id.message, "setBackgroundColor",
android.graphics.Color.BLACK);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)