多种小部件尺寸

发布于 2024-12-28 14:29:35 字数 493 浏览 3 评论 0原文

我在互联网上搜索但没有任何帮助。我想制作第二个小部件,只是更大。我使用服务来更新我的小部件,因此我需要创建 2 个服务(通过远程视图等更新小部件中的数据),或者我可以使它更容易吗?

当我添加中等小部件时,它会显示一个小的小部件,但它占用更多空间(144dp x 72dp)并且没有更新。也许我应该复制整个应用程序类并修改它,但我认为这是愚蠢的解决方案。

我正在尝试此解决方案

如何在一个应用程序中添加多个小部件?

如何将多个小部件尺寸放入一个中apk?

有任何提示吗? :)

I was searching in internet but nothing helps. I want to make second widget, just bigger. Im using service to update my widget so I need to create 2 services (update data in widget by remoteviews etc.) or can I make it easier?

When I add medium widget, it shows up te small one but it takes more space (144dp x 72dp) and there is no update. Maybe I should copy whole app classes and modify it, but I think this is stupid solution.

I was trying this solutions

how to add multiple widgets in one app?

How to put multiple widget sizes in one apk?

Any hints? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雾里花 2025-01-04 14:29:35

我对我的小部件有同样的需求。正是您发布的这两个链接帮助我找到了解决方案,最终通过更多的研究得出了解决方案。

为了让您的小部件具有第二种尺寸:

  1. 在清单中使用不同的名称定义另一个接收器。这很重要:如果您不使用不同的名称,则可用小部件列表中只会显示一个名称。例如:android:name=".MyWidget_Small"android:name=".MyWidget_Medium"。还要相应地修改标签

  2. 附加接收器需要 res/xml 中的新 appwidget 提供程序。在元数据标签的 android:resource 中,键入另一个 XML 文件的名称。然后,您将拥有 android:resource="@xml/MyWidgetProvider_Small"android:resource="@xml/MyWidgetProvider_Medium"

  3. res/xml 中的每个 XML 文件必须指向一个单独的布局。您将拥有 android:initialLayout="@layout/MyWidget_Small"android:initialLayout="@layout/MyWidget_Medium"

  4. 现在是 Java 部分。每个 appwidget 提供程序都需要其相应的扩展 AppWidgetProvider 的 Java 类,因此您将需要两个额外的类。如果您的原始类是 MyWidget.java,则您将添加 MyWidgetSmall.java 和 MyWidgetMedium.java。您最终可能会对原始 Java 类进行子类化,并且如果小部件在每种尺寸下都有不同的行为,则可能会修改每个新类。请记住按照步骤 1 中的方法在接收器的 android:name

    中命名这两个类,

  5. 您的服务不需要重复。但是,您应该检查源代码以查找对 MyWidget.class 的显式引用。如果您这样做,则必须引用实际的子类

希望这有帮助

I had the same need with my widget. Precisely those two links you have posted helped me find the solution, which came at last with a bit more of research.

In order to have a second size for your widget:

  1. Define another receiver in your manifest, with a diferent name. This is important: if you don't use a different name, only one will show in the list of available widgets. For example: android:name=".MyWidget_Small" and android:name=".MyWidget_Medium". Also modify the labels accordingly

  2. The additional receiver needs a new appwidget provider in res/xml. In android:resource of the meta-data tag type the name of another XML file. You'll have then android:resource="@xml/MyWidgetProvider_Small" and android:resource="@xml/MyWidgetProvider_Medium"

  3. Each XML file in res/xml must point to a separate layout. You will have android:initialLayout="@layout/MyWidget_Small" and android:initialLayout="@layout/MyWidget_Medium"

  4. Now the Java part. Each appwidget provider need its correspondent Java class that extends AppWidgetProvider, so you will need two additional classes. If your original class was MyWidget.java, you'll add MyWidgetSmall.java and MyWidgetMedium.java. You'll probably end up subclassing your original Java class, and perhaps modifing each new class if the widget has a distinct behavior in each size. Remember to name the two classes as you did in step 1 in the receivers's android:name

  5. Your service doesn't need duplication. However you should examine your source code to find occurrences of explicit references to MyWidget.class. If you did this, you must reference the actual subclass

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文