gpt4 book ai didi

android - RemoteView addView 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:33 24 4
gpt4 key购买 nike

我有一个应用程序小部件,我想将 View (TextView 等)添加到 RemoteView,但它从未显示。
代码如下:

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

有什么想法吗?


这就是我最终做的:

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

最佳答案

addView() 方法需要您要将此新 View 添加到的布局内 View 的 ID,而不是布局本身。

取而代之的是:

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

试试这个:

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

假设您的布局看起来像这样:

文件: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>

关于android - RemoteView addView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4078039/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com