现代 Android 小部件开发
Modern Android Widget Development
现在是 2020 年,在 iOS 终于添加了对小部件的支持后,小部件再次风靡一时。但是,Android 小部件似乎自 2012 年以来就没有更新过。
来自Android docs:
A RemoteViews object (and, consequently, an App Widget) can support
the following layout classes:
- FrameLayout
- LinearLayout
- RelativeLayout
- GridLayout
这意味着,没有 ConstraintLayout,没有 RecyclerView,没有 AndroidX。不想回到ListView的黑暗岁月
Android 开发人员站点上的文档真的仍然是当今开发 Android 小部件的最佳实践吗?有没有人有在 AndroidX 世界中制作小部件的经验?是否有任何类型的第 3 方 API 可以减轻这种体验?
However, Android Widgets don't seem to have been updated since 2012.
正确。
I don't want to go back to the dark days of ListView.
嗯……抱歉?
Is the documentation on the Android developer site really still the best practice for developing Android Widgets today?
应用程序小部件不是关于“最佳实践”,而是更多关于记录和支持的协议。请记住,应用程序小部件实际上是一种进程间通信 (IPC) 协议,最终恰好导致 UI。您的应用未呈现 UI — 主屏幕或其他应用小部件主机通常在 AppWidgetHost
和相关 API 的帮助下呈现。
Which means, no ConstraintLayout, no RecyclerView, no AndroidX.
正确。毕竟,您不知道主屏幕是否可以访问任何这些东西,或者它们的版本等等。
Is there any kind of 3rd party API to make the experience less painful?
这真的不可能。您的应用程序与应用程序小部件几乎没有关系。 “繁重的工作”在框架中,在较小程度上在主屏幕中。第三方 API 无法真正改变这一点。
我期待 Google 开始引导启动器开发人员开始支持 slices as an alternative form of "app widget". That has not happened yet, as Google seems to have forgotten about slices. Perhaps, in the future, Google will offer something based on Android 11's SurfaceControlViewHost
,这为各种可能性打开了大门。在这两种情况下,可能都需要启动器开发人员进行更改以支持这些东西,这会减慢采用速度。
除了有限的小部件和容器列表之外,您可以做的最接近 UI 的事情是将您自己的东西渲染到 Bitmap
并在 ImageView
在应用程序小部件中。这变得棘手,因为您不知道应用程序小部件的精确大小。当然,这不会改变输入选项。
现在是 2020 年,在 iOS 终于添加了对小部件的支持后,小部件再次风靡一时。但是,Android 小部件似乎自 2012 年以来就没有更新过。
来自Android docs:
A RemoteViews object (and, consequently, an App Widget) can support the following layout classes:
- FrameLayout
- LinearLayout
- RelativeLayout
- GridLayout
这意味着,没有 ConstraintLayout,没有 RecyclerView,没有 AndroidX。不想回到ListView的黑暗岁月
Android 开发人员站点上的文档真的仍然是当今开发 Android 小部件的最佳实践吗?有没有人有在 AndroidX 世界中制作小部件的经验?是否有任何类型的第 3 方 API 可以减轻这种体验?
However, Android Widgets don't seem to have been updated since 2012.
正确。
I don't want to go back to the dark days of ListView.
嗯……抱歉?
Is the documentation on the Android developer site really still the best practice for developing Android Widgets today?
应用程序小部件不是关于“最佳实践”,而是更多关于记录和支持的协议。请记住,应用程序小部件实际上是一种进程间通信 (IPC) 协议,最终恰好导致 UI。您的应用未呈现 UI — 主屏幕或其他应用小部件主机通常在 AppWidgetHost
和相关 API 的帮助下呈现。
Which means, no ConstraintLayout, no RecyclerView, no AndroidX.
正确。毕竟,您不知道主屏幕是否可以访问任何这些东西,或者它们的版本等等。
Is there any kind of 3rd party API to make the experience less painful?
这真的不可能。您的应用程序与应用程序小部件几乎没有关系。 “繁重的工作”在框架中,在较小程度上在主屏幕中。第三方 API 无法真正改变这一点。
我期待 Google 开始引导启动器开发人员开始支持 slices as an alternative form of "app widget". That has not happened yet, as Google seems to have forgotten about slices. Perhaps, in the future, Google will offer something based on Android 11's SurfaceControlViewHost
,这为各种可能性打开了大门。在这两种情况下,可能都需要启动器开发人员进行更改以支持这些东西,这会减慢采用速度。
除了有限的小部件和容器列表之外,您可以做的最接近 UI 的事情是将您自己的东西渲染到 Bitmap
并在 ImageView
在应用程序小部件中。这变得棘手,因为您不知道应用程序小部件的精确大小。当然,这不会改变输入选项。