少来点BUG吧 2025-12-09 18:18 采纳率: 0%
浏览 2

appwidget-provider

小组件在切换主题之后会消失

   <receiver
            android:name="com.android.alarmclock.AnalogAppWidgetProvider"
            android:exported="true"
            android:label="@string/analog_gadget">
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

                <action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
                <action android:name="android.appwidget.action.APPWIDGET_RESTORED" />
                <action android:name="android.intent.action.CONFIGURATION_CHANGED" />
            </intent-filter>

           <!--
            <action android:name="android.intent.action.WALLPAPER_CHANGED" />
                <action android:name="android.service.wallpaper.WallpaperService.ACTION_WALLPAPER_CHANGED" />-->
            <meta-data
                android:name="android.appwidget.oldName"
                android:value="com.android.deskclock.AnalogAppWidgetProvider" />
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/analog_appwidget_v1" />
        </receiver>

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-12-09 18:28
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    问题分析

    你遇到的问题是:小组件在切换主题之后会消失。

    解决方案

    这是因为小组件的布局文件中使用了主题相关的资源(如颜色、字体等),当主题切换时,这些资源会改变,导致小组件的布局发生变化,进而导致小组件消失。

    解决方法

    1. 使用android:exported="true"属性

    确保小组件的AndroidManifest.xml文件中有android:exported="true"属性。

    <appwidget-provider
        android:minWidth="40dp"
        android:minHeight="40dp"
        android:updatePeriodMillis="1800000"
        android:initialLayout="@layout/layout"
        android:resizeMode="horizontal|vertical"
        android:exported="true"
        android:configure="com.example.MyAppWidgetConfigureActivity"
        android:widgetCategory="home_screen"
        android:previewImage="@drawable/widget_preview"
        android:widgetId="1">
    </appwidget-provider>
    
    1. 使用android:theme属性

    在小组件的布局文件中使用android:theme属性来指定主题。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
        <!-- 小组件的布局 -->
    </LinearLayout>
    
    1. 使用android:background属性

    在小组件的布局文件中使用android:background属性来指定背景颜色或图片。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">
        <!-- 小组件的布局 -->
    </LinearLayout>
    

    注意

    上述解决方法仅供参考,具体解决方案可能需要根据你的项目需求进行调整。

    评论

报告相同问题?

问题事件

  • 创建了问题 12月9日