1

xml で定義された形状をウィジェットの画像の背景として設定するにはどうすればよいですか? これが私の試みです:

RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);

Drawable dr = getResources().getDrawable(R.drawable.widgetshape);  //class cast exception
Bitmap bp = ((BitmapDrawable)dr).getBitmap();
views.setImageViewBitmap(R.id.ImageView01, bp);

これにより、android.graphics.drawable.GradientDrawable クラスのキャスト例外が発生します。

Bitmap icon = BitmapFactory.decodeResource(WidgetConfig.this.getResources(),  R.drawable.widgetshape);
views.setImageViewBitmap(R.id.ImageView01, icon);

これにより、行に NullPointerException エラーがスローされますawm.updateAppWidget(awID, views);

Bitmap bp = ((BitmapDrawable)this.getResources().getDrawable(R.drawable.widgetshape)).getBitmap(); //Class Cast Exception
views.setImageViewBitmap(R.id.ImageView01, bp);

これにより、最初の行で android.graphics.drawable.GradientDrawable クラスのキャスト例外が発生します。

4

1 に答える 1

1

インスタンスsetIntで呼び出すメソッドを使用してこれを行うことができます。RemoteViews

views.setInt(R.id.layout, "setBackgroundResource", R.drawable.widgetshape);

これを機能させるには、ウィジェットレイアウト(xml)の(おそらくルートビュー)に@id属性を設定する必要があります。"layout"View

次のように、xmlからそのビューの背景を定義することもできます。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/widgetshape">
于 2012-05-16T16:07:02.077 に答える