代码之家  ›  专栏  ›  技术社区  ›  Bruno Berisso

android textview.settext不适用于简单的小部件

  •  3
  • Bruno Berisso  · 技术社区  · 15 年前

    我有一个非常简单的测试小部件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");
        }
    
    }
    

    测试布局如下:

    <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 回复  |  直到 14 年前
        1
  •  9
  •   Raunak    14 年前

    您忘记设置要使用的远程视图。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);
        }
    
    }