如何在一个应用程序添加多个窗口小部件? [英] how to add multiple widgets in one app?

查看:265
本文介绍了如何在一个应用程序添加多个窗口小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成我的Andr​​oid窗口小部件。现在,我需要有不同的尺寸本wiget的从用户选择。比如我需要一个中型,小型和大型部件。因此,当用户安装应用程序并按住主屏幕,然后选择部件,在小部件菜单我希望他能看到三个小部件相同的应用程序名称,但随着大小。是这样的:

I've just finished my Android widget. Now I need to have different sizes of this wiget for the user to choose from. for example I need a medium, small and large size widget. so when the user install the app and hold the the home screen then choose widget, in the widget menu I want him to see three widget with the same app name but with the size. something like this:

helloSmall helloMedium helloLarge

helloSmall helloMedium helloLarge

我有介质上的一个准备好,但我怎样才能使小和大的相同的应用程序?知道所有三种尺寸含有完全相同的数据和动作只是大小和背景不同。

I have the medium one ready but how can I make the small and the large in the same app? knowing that all three sizes contain the same exact data and actions just the size and the background are different.

感谢。

推荐答案

您在manifest文件一样需要为每个类型的接收器的定义:

You need a receiver definition for each type in your manifest file like:

    <receiver android:name=".MyWidget" android:label="@string/medium_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/medium_widget_provider" />
    </receiver>

    <receiver android:name=".MyWidget" android:label="@string/large_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/large_widget_provider" />
    </receiver>

这将允许你有相同的 AppWidgetProvider 类可用于多种部件,具有在中所定义的不同部件的名称和大小不同; appwidget-提供商GT; XML

This would allow you to have the same AppWidgetProvider class be used for multiple widgets, with different widget names and different sizes defined in the <appwidget-provider> XML.

现在,如果你需要在你的部件比什么是更差的&LT; appwidget提供者&GT; XML我想创建一个实现所有常见的behavoir基本小部件类不同类型之间:

Now if you need more differences in your widgets than what is in the <appwidget-provider> XML I would create a base widget class that implements all the common behavoir between the different types:

public abstract class MyBaseWidget extends AppWidgetProvider

然后你的每个具体实现的可扩展MyBaseWidget。然后在你的清单文件,你就必须为每个具体实现的接收器的定义,如:

And then each of your concrete implementations could extend MyBaseWidget. Then in your manifest file you would have a receiver definition for each of your concrete implementations like:

    <receiver android:name=".MyMediumWidget" android:label="@string/medium_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/medium_widget_provider" />
    </receiver>

    <receiver android:name=".MyLargeWidget" android:label="@string/large_widget_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/large_widget_provider" />
    </receiver>

这篇关于如何在一个应用程序添加多个窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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