未显示在小部件中

未显示在小部件中

@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);

    }

最佳答案

<?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>

关于android - ImageView 未显示在小部件中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22785354/

10-10 20:03