我想创建一个AppWidget,其中TextView的背景颜色随机以指定的周期性间隔更改。Android 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找不到任何 视图,用错误观点
android.widget.RemoteViews $ ActionException: 观点:android.widget.TextView不能 使用方法与RemoteViews: setBackgroundResource(int)
正是。您只能使用具有@RemotableViewMethod注释的方法 – 2013-12-12 22:08:56