我想显示一个小部件,它在小部件空间中加载一个已经工作的 fragment 。我似乎找不到任何处理将 fragment 加载到小部件中的文档?谁能帮帮我?
widget_current.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/widgetCurrent" />
</LinearLayout>
widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/widget_current"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>
UpcomingEventWidgets.kt
class UpcomingEventsWidget: AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
val remoteView = RemoteViews(context.packageName, R.layout.widget_current)
appWidgetManager.updateAppWidget(appWidgetIds, remoteView)
}
}
如何将 fragment 加载到 remoteView 中?
最佳答案
首先,请查看此文档:https://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
本节专门解释了小部件布局可以使用的内容,它为您提供了一个包含所有可用布局类和小部件类的列表。如果列表中没有任何内容,它将无法在小部件上运行。
如果您可以深入了解类源文件,您应该会看到其中的一些 - @RemoteView 在开头,这意味着该类适用于小部件;如果一个类没有这个标记,则表示您不能在小部件中使用它。
关于android - 在 android 小部件中显示 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37632536/