Android:小部件无法在主屏幕上显示

时间:2013-07-04 22:15:10

标签: android widget android-studio homescreen

我一直在关注官方Android开发者页面上的App Widgets教程: http://developer.android.com/guide/topics/appwidgets/index.html

我在'小部件'中看到小部件,当我将其添加到主主屏幕时,它会加载指定的活动,但视图不会显示在主屏幕上。

这是布局:

<?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"
          android:gravity="center"
          android:background="@drawable/ic_launcher">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:text="My Widget..." />

<Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|center"
    android:text="My test button" />

</LinearLayout>

以下是应用小部件信息:

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
                android:minWidth="72dp"
                android:minHeight="72dp"
                android:updatePeriodMillis="86400000"
                android:previewImage="@drawable/ic_launcher"
                android:initialLayout="@layout/widget_main"
                android:configure=".MainActivity"
                android:resizeMode="horizontal|vertical"
                android:widgetCategory="home_screen|keyguard"
                android:initialKeyguardLayout="@layout/widget_main">
</appwidget-provider>

以下是清单中的接收者:

<receiver android:name="MyWidget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.appwidget.action.ACTION_WIDGET_RECEIVER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
                   android:resource="@xml/mywidget_info" />
    </receiver>

1 个答案:

答案 0 :(得分:1)

来自App Widgets Guide on Configuring Activity

  • App Widget主机调用配置Activity,配置Activity应始终返回结果。结果应包括启动活动的Intent传递的App Widget ID(在Intent extras中保存为EXTRA_APPWIDGET_ID)。

首次更新小部件时(从guide开始),您确定使用

Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);

在致电finish()之前?

相关问题