图像视图未出现在窗口小部件中

时间:2014-04-01 12:05:06

标签: android android-widget

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
            MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {

        mRemoteViews = new RemoteViews(context.getPackageName(),
                R.layout.widget_layout);
        mRemoteViews.setImageViewResource(R.id.photo, R.drawable.photo1);

        // Register an onClickListener
        Intent intent = new Intent(context, MyWidget.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, mRemoteViews);

    }

1 个答案:

答案 0 :(得分:2)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dip"
    android:orientation="vertical"
     >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/photo1"/>

</LinearLayout>

try using this as your layout i have checked its working properly.
and make it this file as your app widget provider keep it in res/xml/ directrory

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/widget_layout"
    android:minHeight="72dp"
    android:minWidth="300dp"
    android:updatePeriodMillis="300"
    android:widgetCategory="keyguard|home_screen" >

</appwidget-provider>