【问题标题】:Android: Changing LinearLayout in a widgetAndroid:在小部件中更改 LinearLayout
【发布时间】:2010-04-28 01:30:28
【问题描述】:

我有一个非常烦人的问题:

在我的小部件中,我想通过代码更改背景。我在 Google 文档上注意到,我可以轻松更改 Imageview 的背景:

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

好的,太容易了,我现在想更改线性布局。我读到的有关远程视图 ID 的内容,我可以更改位图、Int、Bool、字符串等……但不能更改可绘制对象。所以我想我不能使用:

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

我完全失望并尝试了最后一个想法:

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

但是日志猫告诉我:

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

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

非常感谢任何帮助。

非常感谢!

【问题讨论】:

    标签: java android


    【解决方案1】:

    由于RemoteViews API 的限制,您无法以这种方式修改背景。

    当您为此特定更新创建 RemoteViews 实例时,您可以选择一种不同的布局 - 具有所需背景的布局。

    【讨论】:

    • 是的,我没有想到这一点,这将是一个完美的解决方案。不幸的是,我读得太晚了,同时,我用 Framelayout 和 imageview 做了一些东西。我不知道是否有很多差异或更好的方法来提高小部件的性能,但两者似乎都有效。感谢您的回答。
    • 您好,Profete,您能用 FrameLayout 和 ImageView 描述您的解决方案吗?
    【解决方案2】:

    我认为解决方案是这样的:

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

    这将使背景图像很好地适合文本视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 2013-05-05
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      相关资源
      最近更新 更多