android - 在 android 小部件中显示 fragment

标签 android android-fragments widget fragment kotlin

我想显示一个小部件,它在小部件空间中加载一个已经工作的 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/

相关文章:

android - 如何在android中播放、暂停和停止歌曲?

java - 如何在不声明权限的情况下读取内部存储中的文件?

android - 从华为 AppGallery 下载该应用程序时,Facebook Audience Network 广告不起作用

java - Camera2 API 使预览填充整个 View

dart - Flutter 中带有调整下拉箭头图标的全宽 DropdownButton

java - 如何将 Long[] 写入包裹?

android - 带有 Activity 的 ViewPager 和 Fragment 生命周期

java - 删除另一个 fragment Activity 中的 fragment

ios - 是否可以确定 iOS 小部件何时隐藏?

qt - 如何在qt中设置小部件的绝对位置