动态地将自定义视图添加到RemoteViews [英] Dynamically adding a custom view to RemoteViews

查看:211
本文介绍了动态地将自定义视图添加到RemoteViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么可以帮助我做到这一点的吗?我的代码就像:

Could any help me to do this? My code is like:

 public CustomClass extends View {

//uses ondraw() to do something

}

为了在主屏幕上显示我的自定义视图,我创建了一个类来扩展Broadcast Receiver:

For displaying my custom view on the home screen I created a class to extend Broadcast Receiver:

public class customAppWidgetProvider extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.main);

           //Here I want to create my custom view class object and I want to add this view to linear layout in main.xml

              CustomClass object = new CustomClass(context) ;
              LinearLayout layout = new LinearLayout(context) ;
              layout.setLayoutParameters(new LayoutParameters(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
              layout.addView(object);

            views.addview(R.id.linearlayout, (ViewGroup) layout) ;
            views.setOnClickPendingIntent(R.id.analog_appwidget,
                    PendingIntent.getActivity(context, 0,
                        new Intent(context, AlarmClock.class),
                        PendingIntent.FLAG_CANCEL_CURRENT));

            int[] appWidgetIds = intent.getIntArrayExtra(
                    AppWidgetManager.EXTRA_APPWIDGET_IDS);

            AppWidgetManager gm = AppWidgetManager.getInstance(context);
            gm.updateAppWidget(appWidgetIds, views);
        }
    }
}

但是将ViewGroup添加到RemoteView引用不起作用...上面的main.xml布局仅包含LinearLayout.我想向其中添加一个自定义视图对象.但是运行此命令后,屏幕上没有任何显示...

But adding ViewGroup to RemoteView reference is not working... The main.xml layout above contains only LinearLayout. I want to add a custom view object to it. But after running this nothing shows on screen...

请帮助我做到这一点.预先感谢.

Please help me to do this. Thanks in Advance.

推荐答案

无法向应用程序小部件添加自定义View.请参阅应用程序的创建应用程序小部件布局"部分 Widgets开发指南,允许使用View类型.

It's not possible to add a custom View to an app widget. See the "Creating the App Widget Layout" section of the App Widgets Dev Guide for what View types are allowed.

Android 3.0添加了对某些视图的支持,以显示集合.有关详细信息,请参见将App窗口小部件与集合一起使用".

Android 3.0 adds support for some views to display collections. See the "Using App Widgets with Collections" section for details.

否则,要在将RemoteViews夸大并获得对其的引用之后,将允许的View动态添加到App Widget,可以在任何以下工具上使用其addView(View)方法或addView(View)方法RemoteViews中已经有View个对象.

Otherwise, to dynamically add an allowed View to an App Widget, after inflating the RemoteViews and getting a reference to it, you can use its addView(View) method, or the addView(View) method on any of the View objects already in the RemoteViews.

这篇关于动态地将自定义视图添加到RemoteViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆