gpt4 book ai didi

java - 如何从 RemoteView 中的布局编辑/更改 View 或从 View 创建 RemoteView?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:54:44 26 4
gpt4 key购买 nike

我为 Android 应用程序创建小部件(当然是用 Java)。我有从布局(使用布局 ID)创建的经典 RemoteViews

RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.view);

我需要编辑或更改 View (通过 ID 标识)。在经典 View 中很容易,使用 findViewById 函数。

View v = ... //inflate layout R.layout.view
View my = v.findViewById(R.id.myViewId);
processView(my); //filling view

但 RemoteViews 不支持它。可以使用 apply() 获取 View ,但在 processView 和 reapply() 之后我看不到 View 的变化。

View v = rv.apply(context, null);
View my = v.findViewById(R.id.myViewId);
processView(my); //this work's fine
rv.reapply(context,my);

第二个,更糟糕的选择,是从 RemoteViews 获取我需要的 View ,处理它,删除旧 View 并使用 addView() 添加处理过的新 View 。

RemoteViews rv = ...
View my = ... //apply, find and process
//remove old view
RemoteViews rvMy = ... //create RemoteViews from View
rv.addView(rvMy)

但我不知道如何从 View 创建 RemoteView(这可能吗?)。有什么想法可以解决这个问题吗?

最佳答案

试试这个方法:

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);

remoteViews.setTextViewText(R.id.widget_textview, text); <---- here you can set text

// Tell the widget manager
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

这是一篇有助于理解小部件行为的帖子:

http://www.vogella.com/articles/AndroidWidgets/article.html

关于java - 如何从 RemoteView 中的布局编辑/更改 View 或从 View 创建 RemoteView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19884218/

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