android TextView.setText不适用于简单的小部件

时间:2010-11-27 14:34:04

标签: android android-emulator android-widget

我有一个非常简单的AppWidgetProvider用于测试小部件:

public class Test extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_layout);
        views.setTextViewText(R.id.TextView01, "Test message");
    }

}

test_layout如下所示:

<LinearLayout 
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/TextView01" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </TextView>
</LinearLayout>

问题是小部件出现在模拟器屏幕中但没有任何文本。我敢肯定,我正在弄乱一些东西,但我找不到它是什么......

1 个答案:

答案 0 :(得分:9)

您忘记设置要使用的RemoteView。您的AppWidgetProvider代码应为:


public class Test extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_layout);
        views.setTextViewText(R.id.TextView01, "Test message");
        appWidgetManager.updateAppWidget(appWidgetIds, views);
    }

}