小编Pel*_*lly的帖子

如何在Android中以编程方式更改小部件布局背景

以此小部件布局为例(我的整个小部件布局的一部分)

<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"
>
Run Code Online (Sandbox Code Playgroud)

我希望能够根据用户选择更改使用的背景drawable.例如,使用远程视图,我可以通过执行以下操作来更新textview的颜色:

remoteView.setTextColor(R.id.text_view1, Color.WHITE);
Run Code Online (Sandbox Code Playgroud)

然而,我发现我的线性布局背景很难相同.我试过这个:

remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable", ((BitmapDrawable) context.getResources().getDrawable(R.drawable.widget_background_dark_4x2)).getBitmap());
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

<<编辑>>也试过这个:

Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(R.drawable.widget_background_light_4x2)).getBitmap();
remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable",bitmap );
Run Code Online (Sandbox Code Playgroud)

但不幸的是得到以下错误:

06-01 23:11:26.039: WARN/AppWidgetHostView(244): updateAppWidget couldn't find any view, using error view 06-01 23:11:26.039: WARN/AppWidgetHostView(244): android.widget.RemoteViews$ActionException: view: android.widget.LinearLayout doesn't have method: setBackgroundDrawable(android.graphics.Bitmap)

layout android background widget

13
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

background ×1

layout ×1

widget ×1