RemoteView addView non funziona
-
28-09-2019 - |
Domanda
Ho un widget di app e mi piacerebbe aggiungere Visualizzazioni (TextView
, ecc,) per l'RemoteView
ma non è mai presenta.
Qui va il codice:
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);
Tutte le idee?
Questo è quello che ho finito per fare:
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);
Soluzione
Il metodo addView () ha bisogno l'id della vista all'interno del layout che si desidera aggiungere questo nuovo vista, non il layout stesso.
Al posto di questo:
views.addView(views.getLayoutId(), newView);
Prova questo:
views.addView(R.id.view_container, newView);
Supponendo che il layout simile a questa:
file: Layout / 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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow