远程视窗addView不工作 [英] RemoteView addView not working
本文介绍了远程视窗addView不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个应用程序窗口小部件,我想补充意见(的TextView
等)的远程视窗
,但它一直没有出现。
这里所说的code:
RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.widget_layout);
RemoteViews NewView的=新RemoteViews(context.getPackageName(),R.layout.widget_row_layout);
newView.setTextViewText(R.id.textUser,1234);
views.addView(views.getLayoutId(),NewView的);
//告诉AppWidgetManager对当前应用程序窗口小部件进行更新
appWidgetManager.updateAppWidget(appWidgetId,意见);
任何想法?
这是我落得这样做:
RemoteViews NewView的=新RemoteViews(context.getPackageName(),R.layout.widget_row_layout);
newView.setTextViewText(R.id.textUser,1234);
组件名thisWidget =新的组件名(本,WidgetProvider.class);
AppWidgetManager经理= AppWidgetManager.getInstance(本);
manager.updateAppWidget(thisWidget,NewView的);
解决方案
在addView()方法需要您要添加这个新的视图,而不是布局本身布局内的视图的ID。
取而代之的是
views.addView(views.getLayoutId(),NewView的);
试试这个:
views.addView(R.id.view_container,NewView的);
假设你的布局看起来是这样的:
文件:布局/ widget_layout.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
<的LinearLayout
机器人:ID =@ + ID / view_container
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
<! - 新的观点将在这里添加在运行时 - >
< / LinearLayout中>
< / LinearLayout中>
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);
解决方案
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>
这篇关于远程视窗addView不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文