2010-04-28 25 views
0

我有这个真正烦人的问题:Android:更改控件中的LinearLayout

在我的小工具中,我想通过代码更改背景。我注意到了谷歌文档比我可以很容易地改变ImageView的背景:

remoteViews.setImageViewResource(R.id.my_iv, R.drawable.my_bg); 

好,太容易了,我现在要改变线性布局..我看怎么样远程视窗的ID我可以改变Bitmap,Int,Bool,String等等,但不是可绘制的。所以我想我不能使用:

remoteViews.setBitmap(R.id.my_ll,"setBackgroundDrawable",BitmapFactory.decodeResource(context.getResources(), R.drawablemy_bg)); 

我完全disapointed并试图最后的想法:

views.setInt(R.id.my_ll,"setBackgroundResource",R.drawable.my_bg); 

但logcat的告诉我:

android.widget.RemoteViews $ ActionException :view: android.widget.LinearLayout不能与RemoteViews一起使用 方法: setBackgroundResource(int)

我完全失去了,我真的不知道该怎么办...

我真的很感激任何帮助。

非常感谢!

回答

2

由于RemoteViews API的局限性,您无法修改背景。

当您为此特定更新创建RemoteViews实例时,您可以执行的操作是选择不同的布局 - 一个具有您想要的背景。

+0

是的,我没有想到这,这本来是一个完美的解决方案。 不幸的是,我读了你太晚了,与此同时,我做了一个Framelayout和一个imageview。 我不知道是否有很多差异或更好的方法来改善小部件的性能,但两者似乎都有效。 感谢您的回答。 – 2010-04-28 16:33:39

+0

嗨Profete,你能用FrameLayout和ImageView来描述你的解决方案吗? – w00d 2011-01-10 16:56:03

1

我认为解决的办法是这样的:

<FrameLayout android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView android:id="@+id/label_bg" android:src="@drawable/widget_label_white" 
     android:scaleType="fitXY" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:layout_gravity="center"/> 

    <TextView android:gravity="center" android:id="@+id/label" 
     android:text="Processes" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:textColor="#ffffffff" 
     android:shadowColor="#ff000000" android:shadowDx="1" 
     android:shadowDy="1" android:shadowRadius="1" /> 


</FrameLayout> 

这将使背景图像很好地适应TextView的。