以这个小部件布局为例(我整个小部件布局的一部分)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widget_background_dark_4x2"
android:orientation="horizontal"
android:id="@+id/widget_main"
>
我希望能够根据用户的选择更改使用的背景可绘制对象。例如,使用远程 View ,我可以通过执行以下操作来更新 TextView 的颜色:
remoteView.setTextColor(R.id.text_view1, Color.WHITE);
然而,我一直发现我的线性布局的背景很难相同。我试过这个:
remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable", ((BitmapDrawable) context.getResources().getDrawable(R.drawable.widget_background_dark_4x2)).getBitmap());
但是我得到以下错误:
06-01 22:46:36.305: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setBackgroundDrawable(android.graphics.Bitmap)
<< 编辑 >> 也试过这个:
Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background_light_4x2)).getBitmap();
remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable",bitmap );
但不幸的是得到以下错误:
06-01 23:11:26.039: WARN/AppWidgetHostView(244): updateAppWidget 找不到任何 View ,使用错误 View
06-01 23:11:26.039: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout 没有方法:setBackgroundDrawable(android.graphics.Bitmap)
最佳答案
你试过吗:
remoteView.setInt(R.id.widget_main, "setBackgroundResource", R.drawable.widget_background_dark_4x2);
关于android - 如何在 Android 中以编程方式更改小部件布局背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6201410/