我想創建一個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