如何在同一个应用程序中添加多个小部件? [英] How to add multiple widgets in the same app?

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

问题描述

我刚刚完成了Android小部件.现在,我需要使该小部件具有不同的大小,以供用户选择.

I've just finished my Android widget. Now I need to have different sizes of this widget for the user to choose from.

例如,我需要一个大小适中的小部件,因此当用户安装该应用并按住主屏幕然后选择小部件时,我希望他在小部件菜单中看到三个具有相同应用名称的小部件,但是与大小.像这样:

For example, I need a medium, small and large size widget, so when the user installs the app and hold the home screen then choose widget, in the widget menu I want him to see three widgets with the same app name but with the size. Something like this:

你好小 你好中 helloLarge

helloSmall helloMedium helloLarge

我已经准备好中号了,但是如何在同一个应用程序中添加大小号呢?知道这三个大小都包含相同的确切数据和操作,只是大小和背景不同.

I have the medium one ready, but how can I add 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.

推荐答案

对于清单文件中的每种类型,您都需要一个接收者定义,例如:

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-provider> 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.

现在,如果您需要的小部件差异比<appwidget-provider> XML中的差异大,我将创建一个基本的小部件类,该类实现不同类型之间所有常见的行为:

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天全站免登陆