【问题标题】:Android AppWidget: Add Custom Views to Widget LayoutAndroid AppWidget:向小部件布局添加自定义视图
【发布时间】:2013-07-02 09:48:02
【问题描述】:

我想在我的 Widget 中显示一些 TextView 和 EditView 列表。

我使用以下代码生成 TextView 和 EditView 的列表

public LinearLayout getMainBodyLayout(List<Item> data) {
        LinearLayout mainLL = new LinearLayout(context);
        mainLL.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < data.size(); i++) {
            Item ritem = data.get(i);
            LinearLayout item = new LinearLayout(context);
            TextView name = new TextView(context);
            EditText nos = new EditText(context);
            name.setText(ritem.getName());
            nos.setText(ritem.getNo());
            item.addView(name);
            item.addView(nos);
            mainLL.addView(item);

        }

        return mainLL;
    }

public void updateWidget(LinearLayout ll) {
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        ComponentName thisWidget = new ComponentName(context, MainWidget.class);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_my);
        remoteViews.setTextViewText(R.id.title, title);

        remoteViews.setTextViewText(R.id.widget_error, errorMsg);

// I ve to add ll to this remoteViews


        manager.updateAppWidget(thisWidget, remoteViews);
    }

我必须将返回的LinearLayout 添加到我的小部件Linearlayout。 请为我提供最好的方法。

【问题讨论】:

  • 你要列个清单吗????如果是这样,为什么不使用列表视图来解决任何问题?而且 listview 会比你现在做的更快。
  • @BBdev,我不知道如何设置适配器...
  • @BBdev 你能告诉我如何用 ListView 做到这一点
  • @Sridhar 为了在 appwidgets 中使用 ListViews 看看developer.android.com/guide/topics/appwidgets/…

标签: android android-appwidget


【解决方案1】:

尝试在 XMl 中制作 Layout 并在 Java 文件中设置其值... 及使用方法

          RemoteViews.setTextviewText(id,string);

试试这个方法,因为我在我的小部件中使用它

        private class ABC extends TimerTask {
         RemoteViews remoteViews;
         AppWidgetManager appWidgetManager;
         ComponentName thisWidget;

         public ABC(Context context, final AppWidgetManager appWidgetManager) {
         this.appWidgetManager = appWidgetManager;


         remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
         thisWidget = new ComponentName(context, WidgetAnimate.class);
          }

         public void run() {

           String k= XMLfunctions.data1();   
           remoteViews.setTextViewText(R.id.tv12,k);
           appWidgetManager.updateAppWidget(thisWidget, remoteViews);

         }

【讨论】:

  • 我想添加自定义视图。我不能使用硬编码没有子视图
【解决方案2】:

为您提供完整的代码会更大。我给你一些代码示例,你可以根据需要对其进行优化。

Android ListView example

这也是一个很好的入门教程

Android ListView Tutorial

如果您想优化您的列表,您可以查看本教程。

ListView Tips & Tricks #3: Create Fancy ListViews

【讨论】:

  • 谢谢,但我问你如何在 AppWidgetProvider 类中设置适配器。
  • 如果你会使用 ListView,你可以直接将适配器设置为 listview。列表视图是Android提供的一个小部件,通过列表视图可以很好地实现查看自己在做什么。
  • 如何获取listview对象
  • RemoteView 是否提供任何特殊方法,如 findViewByID()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-23
  • 1970-01-01
相关资源
最近更新 更多