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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19884218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:01:42  来源:igfitidea点击:

How edit/change View from layout in RemoteView or create RemoteView from View?

javaandroidwidget

提问by honzakuzel1989

I create widget for Android application (in Java, of course). I have classic RemoteViews created from layout (using layout id)

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

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

and I need edit or change view (identify by id). In classic View is it easy, using findViewById function.

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

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

But it is not supported in RemoteViews. Is possible get view using apply(), but after processView and reapply() I not see changes in view.

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

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

Second, worse option, is get my required view form RemoteViews, process it, delete old view and add processed new view using addView().

其次,更糟糕的选择是获取我需要的视图表单 RemoteViews,处理它,删除旧视图并使用 addView() 添加处理过的新视图。

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

But I don't know how create RemoteViews from View (it is possible?). Any ideas how solve this problem?

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

采纳答案by honzakuzel1989

Because edit/change (sub)view from removeview or creating remoteview from view is propably not possible (on base information which I founded) I solved my problem using namingnecessery view (most ofen textview), geting views id using his names and reflection and process in cycle. For naming is possible to use bash, python, or anything else.

因为从删除视图编辑/更改(子)视图或从视图创建远程视图可能是不可能的(根据我创建的基本信息),我使用命名必要视图(最常见的文本视图)解决了我的问题,使用他的名字和反射获取视图 ID 和循环过程。命名可以使用 bash、python 或其他任何东西。

Example:

例子:

RemoteView rv = ...

/* 
exemplary rv layout:
+-----+-----+-----+-----+-----+-----+
|tv0x0|tv0x1|tv0x2|tv0x3|tv0x4|tv0x5|
+-----+-----+-----+-----+-----+-----+
|tv1x0|tv1x1|tv1x2|tv1x3|tv1x4|tv1x5|
+-----+-----+-----+-----+-----+-----+
*/

String prefix = "tv";
for(int i=0; i<2;i++)
{
    for(int j=0; j<6; j++)
    {
        // use reflection, searched in stackoverflow
        int id = getItemIdFromName(prefix+i+"x"+j); 
        // working with concrete id using RemoteView set functions, e.g
        rv.setTextViewText(id, String.ValueOf(i);
    }
}

This way is possible process a large number of views and apply remoteview function for them.

这种方式可以处理大量视图并为其应用远程视图功能。

回答by Angudroid

Try this way :

试试这个方法:

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

Here is a useful post to understand widget behaviour :

这是了解小部件行为的有用帖子:

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

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