android - ImageView 未出现在小部件中

标签 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);

    }

最佳答案

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

相关文章:

android - Samsung Galaxy S2 2.3.5+ 不调用 overScrollBy()

android - 使用服务和 BroadcastReceiver 停止观看应用程序

android - 使用 Android 操作栏分享 Intent

android - 如何在 TabActivity 中隐藏选项卡?

java - 如何从我的应用程序将小部件添加到 Android 主屏幕?

android - 极少数情况下出现 "android.database.sqlite.SQLiteException: no such table"错误

java - setOnclicklistener 方法不起作用

安卓 GCM "error":"NotRegistered"

android - 如何处理小部件按钮

android - 了解 BaseAdapter 以及如何使用它们