Pregunta

Tengo un widget de aplicación y me gustaría añadir Vistas (TextView, etc.,) a la RemoteView pero nunca aparece.
Aquí va el código:

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);

¿Alguna idea?


Esto es lo que terminé haciendo:

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);
¿Fue útil?

Solución

El método addView () necesita el id de la visión dentro del diseño que desee añadir este nuevo objeto de, no el diseño en sí.

En lugar de esto:

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

Prueba esto:

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

Asumiendo que su diseño se ve algo como esto:

file: diseño / 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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top