简体   繁体   English

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

[英]How to change widget layout background programmatically in Android

Take this widget layout for example (part of my whole widget layout)以这个小部件布局为例(我的整个小部件布局的一部分)

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

I want to be able to change the background drawable used based on a users selection.我希望能够根据用户选择更改使用的背景可绘制对象。 For example using remote views I can update the colour of a textview by doing something like this:例如,使用远程视图,我可以通过执行以下操作来更新 textview 的颜色:

remoteView.setTextColor(R.id.text_view1, Color.WHITE);

However I have been finding it difficult to the same for the background of my linear layout.然而,我发现我的线性布局的背景很难做到这一点。 I did try this:我确实试过这个:

remoteView.setBitmap(R.id.widget_main, "setBackgroundDrawable", ((BitmapDrawable) context.getResources().getDrawable(R.drawable.widget_background_dark_4x2)).getBitmap());

But I get the following error:但我收到以下错误:

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)

<< EDIT >> Have also tried this: << EDIT >> 也试过这个:

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

But unfortunately get the following error:但不幸的是得到以下错误:

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)

Did you try:你试过了吗:

remoteView.setInt(R.id.widget_main, "setBackgroundResource", R.drawable.widget_background_dark_4x2);

Ok, well the only solution that I could end up coming up with for this was to change the way I was displaying the background image in the LinearLayout.好的,我最终能想到的唯一解决方案是改变我在 LinearLayout 中显示背景图像的方式。 As you can see above I was using the android:background tag to display the image.正如您在上面看到的,我使用 android:background 标签来显示图像。 What I ended up doing was that I removed this and then encased my entire widget layout (which was inside a linearlayout inside a relativelayout. I then just had an ImageView decalred first within the RelativeLayout and then had the rest of the widgets linearlayout sit on top of it. Then within my code I just set the imageview source via the remote view and it worked like a treat.我最终做的是我删除了它,然后将我的整个小部件布局(它位于相对布局内的线性布局内。然后我只在 RelativeLayout 中首先贴上了 ImageView ,然后将小部件线性布局的 rest 放在顶部然后在我的代码中,我只是通过远程视图设置了 imageview 源,它就像一种享受。

Not the most elegant/efficient way of coding it, but it worked!不是最优雅/最有效的编码方式,但它有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM