Android 小部件未呈现,资源 xml 错误?
Android widget not rendering, wrong xml in resource?
我在模拟器上显示小部件时遇到了一些问题。
我创建了 widget_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/email_button"
android:src="@android:drawable/ic_dialog_email"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/search_button"
android:src="@android:drawable/ic_search_category_default"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/map_button"
android:src="@android:drawable/ic_dialog_map"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/info_button"
android:src="@android:drawable/ic_dialog_info"
/>
</LinearLayout>
然后我创建了新的资源文件widget.xml
:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="250dp"
android:minWidth="40dp"
android:previewImage="@drawable/widget_preview"
android:initialLayout="@layout/widget_layout"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>
当然,我已经添加到清单中(在 </activity>
之后和 </application>
之前):
<receiver android:name=".AppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="//HERE MY widget.xml is not seen"/>
</receiver>
首先,我不知道为什么,Android Studio
无法识别我的 xml/widget
,如下所示:
这是我的项目结构:
我相信这就是为什么在我 运行 模拟器后没有显示小部件的原因。我的代码有什么问题?
编辑
在这里,问题出在启动器上。重启后,效果很好。然而,清单中仍然没有看到该文件夹。不知道是不是BUG,如果我手动写的话,就没问题了。
将此添加到清单?
<activity android:name=".ExampleAppWidgetConfigure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
此处,问题出在启动器上。重启后,效果很好。然而,清单中仍然没有看到该文件夹。不知道是不是BUG,如果我手动写的话,就没问题了。
我在模拟器上显示小部件时遇到了一些问题。
我创建了 widget_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/email_button"
android:src="@android:drawable/ic_dialog_email"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/search_button"
android:src="@android:drawable/ic_search_category_default"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/map_button"
android:src="@android:drawable/ic_dialog_map"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/info_button"
android:src="@android:drawable/ic_dialog_info"
/>
</LinearLayout>
然后我创建了新的资源文件widget.xml
:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="250dp"
android:minWidth="40dp"
android:previewImage="@drawable/widget_preview"
android:initialLayout="@layout/widget_layout"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>
当然,我已经添加到清单中(在 </activity>
之后和 </application>
之前):
<receiver android:name=".AppWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="//HERE MY widget.xml is not seen"/>
</receiver>
首先,我不知道为什么,Android Studio
无法识别我的 xml/widget
,如下所示:
这是我的项目结构:
我相信这就是为什么在我 运行 模拟器后没有显示小部件的原因。我的代码有什么问题?
编辑 在这里,问题出在启动器上。重启后,效果很好。然而,清单中仍然没有看到该文件夹。不知道是不是BUG,如果我手动写的话,就没问题了。
将此添加到清单?
<activity android:name=".ExampleAppWidgetConfigure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
此处,问题出在启动器上。重启后,效果很好。然而,清单中仍然没有看到该文件夹。不知道是不是BUG,如果我手动写的话,就没问题了。