Android 如何在同一个应用程序中添加多个小部件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2570004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add multiple widgets in the same app?
提问by BarcaDroid
I've just finished my Android widget. Now I need to have different sizes of this widget for the user to choose from.
我刚刚完成了我的 Android 小部件。现在我需要有不同大小的这个小部件供用户选择。
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:
例如,我需要一个中、小和大尺寸的小部件,所以当用户安装应用程序并按住主屏幕然后选择小部件时,在小部件菜单中我希望他看到三个具有相同应用名称但大小相同的小部件. 像这样的东西:
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.
我准备好了中号,但如何在同一个应用程序中添加小号和大号?知道所有三种尺寸都包含相同的确切数据和动作,只是尺寸和背景不同。
回答by Mark B
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>
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.
这将允许您将相同的AppWidgetProvider
类用于多个小部件,在<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 behavtheitroad between the different types:
现在,如果您的小部件需要比<appwidget-provider>
XML中的更多差异,我将创建一个基本小部件类,该类实现不同类型之间的所有常见行为:
public abstract class MyBaseWidget extends AppWidgetProvider
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:
然后您的每个具体实现都可以扩展 MyBaseWidget。然后在您的清单文件中,您将为每个具体实现提供一个接收器定义,例如:
<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>
回答by korro
Actually, android:name for each widget have to be different. If you will do this as in example, only one widget will be visible in widgets list.
实际上,每个小部件的 android:name 必须不同。如果您按照示例执行此操作,则小部件列表中将只显示一个小部件。
回答by jblz
Guys, I had the same problem.
伙计们,我遇到了同样的问题。
You need to actually add a second widget provider aswell;
您实际上还需要添加第二个小部件提供程序;
<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=**".MyWidget2"** 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>
Enjoy
享受
回答by user308238
Ok so basically you will need:
好的,所以基本上你需要:
layout file fore each widget. ex: main_small.xml, main_medium.xml ...
每个小部件的布局文件。例如:main_small.xml、main_medium.xml ...
in the xml directory add a provider for each widget. ex: small_provider.xml, medium_provider.xml ... and so on (note if you don't have an xml directory add it under the drawable directory).
在 xml 目录中为每个小部件添加一个提供程序。例如:small_provider.xml、medium_provider.xml ...等等(注意,如果你没有xml目录,请将其添加到drawable目录下)。
now what!
怎么办!
define a receiver in the manifest for eachwidget. (just like the example in the main answer)
you can use the same layout or deferent layout. basically this is up to you.
in your provider you should have something like this:
在清单中为每个小部件定义一个接收器。(就像主要答案中的示例一样)
您可以使用相同的布局或不同的布局。基本上这取决于你。
在您的提供者中,您应该有这样的东西:
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="146dip" android:minHeight="138dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main" />
<?xml version="1.0" encoding="utf-8"?> <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="146dip" android:minHeight="138dip" android:updatePeriodMillis="10000" android:initialLayout="@layout/main" />
make sure, for each provider to specify the target layout file you want to use. in this code I'm asking for the file main.xml in the layout directory. for my medium widget for example i'll have another provider with the same exact code but i'll change the last line
确保为每个提供程序指定要使用的目标布局文件。在此代码中,我要求布局目录中的文件 main.xml。例如,对于我的中等小部件,我将有另一个具有相同代码的提供程序,但我将更改最后一行
> android:initialLayout="@layout/medium".
I hope this helps if not let me know and I can upload a working example on my website and you can take a closer look at it. please let me know how it goes.
如果不让我知道,我希望这会有所帮助,我可以在我的网站上上传一个工作示例,您可以仔细查看它。请让我知道它是怎么回事。
best of luck.
祝你好运。
回答by Richard Le Mesurier
Some extra info to the other answers...
其他答案的一些额外信息......
If you are duplicating the files mentioned, and if your widget uses a
Service
to provide some functionality, you might have to duplicate your service.If you duplicate your
Service
, remember to update your manifest with the new service, otherwise the new service won't run...
如果您要复制提到的文件,并且您的小部件使用 a
Service
来提供某些功能,则您可能必须复制您的服务。如果您复制了您的
Service
,请记住使用新服务更新您的清单,否则新服务将无法运行...
This wasted some time for me.
这对我来说浪费了一些时间。
If you use any BroadcastReceiver
to send Intent
s to your duplicate Service
s... don't forget to update that code too:
如果您使用 anyBroadcastReceiver
将Intent
s发送到重复的Service
s... 也不要忘记更新该代码:
- you must now send intents to eachof the services.
- 您现在必须向每个服务发送意图。