Java Android:小部件在列表中不可见
我正在尝试更新现有的安卓4+应用程序,以提供一个小部件。我在上遵循了教程,但未能使其运行。小部件不显示在小部件列表中,因此无法在主屏幕上安装/使用 这是我将小部件添加到应用程序时所做的: 1。将WidgetProvider和设置活动添加到清单文件:Java Android:小部件在列表中不可见,java,android,android-studio,widget,Java,Android,Android Studio,Widget,我正在尝试更新现有的安卓4+应用程序,以提供一个小部件。我在上遵循了教程,但未能使其运行。小部件不显示在小部件列表中,因此无法在主屏幕上安装/使用 这是我将小部件添加到应用程序时所做的: 1。将WidgetProvider和设置活动添加到清单文件: <!-- Widget --> <receiver android:name=".Widget.WidgetProvider" android:label="Example Widge
<!-- Widget -->
<receiver
android:name=".Widget.WidgetProvider"
android:label="Example Widget"
android:icon="@drawable/some_drawable">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidget_info" />
</receiver>
<activity
android:name=".Widget.WidgetSettingsActivity"
android:label="@string/Settings" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
Und das布局在/res/Layout中/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mycompany.myapp.Widget.WidgetSettingsActivity">
<TextView android:text="@string/Settings" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
5。在/res/Layout中添加小部件布局
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/margin_default"
android:layout_marginRight="@dimen/margin_default"
android:layout_marginTop="@dimen/margin_default"
android:layout_weight="20" >
<Button
android:id="@+id/TButton"
style="@style/BigButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/margin_medium"
android:layout_weight="1"
android:text="@string/Test" />
<Button
android:id="@+id/IButton"
style="@style/BigButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin_medium"
android:layout_weight="1"
android:text="@string/New" />
</LinearLayout>
</LinearLayout>
应用程序编译和运行时没有任何错误。但我无法在任何地方访问/查看/使用小部件
我发现了与此主题相关的其他问题,但它们都有不同的解决方案(例如,缺少意图过滤器,安装在SD卡上而不是内部内存中,等等),而这里的情况并非如此
知道我做错了什么吗?知道了!又搜索了两个小时后,我发现了错误。多亏了另一篇文章中的提示: appwidget_info.xml的格式不正确:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
...
android:widgetCategory="home_screen">
</appwidget-provider>
我想知道为什么Android Studio没有显示任何关于此的错误或警告。会节省我很多时间。。。此外,我不知道为什么我会犯与上面问题中链接的用户相同的错误。也许我是从一个糟糕的例子中复制代码的
但是:如果有人遇到同样的问题,请首先检查XML:-)您在哪台设备上测试?还要检查它是否显示在应用程序列表中的设置?我正在测试Nexus 5。“设置/应用”中有“我的应用”的条目。为什么这很重要?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mycompany.myapp.Widget.WidgetSettingsActivity">
<TextView android:text="@string/Settings" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/margin_default"
android:layout_marginRight="@dimen/margin_default"
android:layout_marginTop="@dimen/margin_default"
android:layout_weight="20" >
<Button
android:id="@+id/TButton"
style="@style/BigButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginRight="@dimen/margin_medium"
android:layout_weight="1"
android:text="@string/Test" />
<Button
android:id="@+id/IButton"
style="@style/BigButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin_medium"
android:layout_weight="1"
android:text="@string/New" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android">
...
android:widgetCategory="home_screen">
</appwidget-provider>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="50dp"
android:minHeight="50dp"
android:minResizeHeight="50dp"
android:minResizeWidth="50dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget__content"
android:configure="com.mycompany.myapp.Widget.WidgetSettingsActivity
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>