Question

J'ai un widget app et je voudrais ajouter des vues (TextView, etc.,) à la RemoteView mais il ne se présente jamais.
Ici va le code:

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_row_layout);
    newView.setTextViewText(R.id.textUser, "1234");
    views.addView(views.getLayoutId(), newView);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);

Toutes les idées?


Voici ce que je fini par faire:

RemoteViews newView = new RemoteViews(context.getPackageName(), R.layout.widget_row_layout);
    newView.setTextViewText(R.id.textUser, "1234");
ComponentName thisWidget = new ComponentName(this,WidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
    manager.updateAppWidget(thisWidget, newView);
Était-ce utile?

La solution

La méthode addView () a besoin de l'identifiant de la vue dans la mise en page que vous voulez ajouter cette nouvelle vue, et non pas la mise en page elle-même.

Au lieu de ceci:

views.addView(views.getLayoutId(), newView);

Essayez ceci:

views.addView(R.id.view_container, newView);

En supposant que votre mise en page ressemble à quelque chose comme ceci:

fichier: mise en page / widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/view_container"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <!-- New views will be added here at runtime -->
    </LinearLayout>
</LinearLayout>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top