RemoteView addView not working

爱⌒轻易说出口 提交于 2019-12-18 04:46:12

问题


I have an app widget and I'd like to add Views (TextView, etc.,) to the RemoteView but it never shows up.
Here goes the 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);

Any ideas?


This is what I ended up doing:

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

回答1:


The addView() method needs the id of the view inside the layout you want to add this new view to, not the layout itself.

Instead of this:

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

Try this:

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

Assuming your layout looks something like this:

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>


来源:https://stackoverflow.com/questions/4078039/remoteview-addview-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!