简体   繁体   English

Android AppWidget:将自定义视图添加到小部件布局

[英]Android AppWidget: Add Custom Views to Widget Layout

I want to show some list of TextView and EditView in my Widget. 我想在我的窗口小部件中显示一些TextView和EditView的列表。

I used the following code to generate list of TextView and 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);
    }

I have to add the returned LinearLayout to my widget Linearlayout. 我必须将返回的LinearLayout添加到我的小部件Linearlayout中。 Please provide me the best way to do this. 请提供给我最好的方法。

Try to make Layout in XMl and set its value in Java file... and use method 尝试在XMl中制作Layout并在Java文件中设置其值...并使用方法

          RemoteViews.setTextviewText(id,string);

try this method as i hv use it in my widget 当我在小部件中使用它时,请尝试此方法

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

         }

Giving you a full code for that will be much bigger. 为您提供完整的代码将更大。 I am giving you some code sample you can optimize it as you needed. 我给您一些代码示例,您可以根据需要对其进行优化。

Android ListView example . Android ListView示例

And this one is also a good tutorial to start with 这也是一个很好的教程

Android ListView Tutorial Android ListView教程

And if you want optimized your list you can see this tutorial. 如果您想优化列表,可以查看本教程。

ListView Tips & Tricks #3: Create Fancy ListViews ListView技巧与窍门#3:创建精美的ListViews

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM