【问题标题】:How to change widget background using RemoteViews?如何使用 RemoteViews 更改小部件背景?
【发布时间】:2012-01-20 01:26:13
【问题描述】:
<RelativeLayout
    android:id="@+id/widget"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_background"
    android:orientation="vertical" >

现在我想使用 RemoteViews 将此背景更改为“@drawable/your_bck”。我试过这样的东西

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.yout_bck);
remoteViews.setImageViewBitmap(R.id.widget, bitmap);

但我随后显示“小部件无法加载”

它必须作为背景,因为我需要在图像的中心设置一个文本:)

【问题讨论】:

    标签: java android android-layout android-widget


    【解决方案1】:

    您正在通过RelativeLayout 呼叫setImageViewBitmapRelativeLayout 不是 ImageView。你可以在里面放一个ImageView,而不是填充RelativeLayout,然后调用它。

    【讨论】:

    • 我明白了,但我需要在这张图片的中心放置一些文本。我想做一些类似电池小部件的东西。 :)
    • 好的,所以在 ImageView 的顶部居中放置一个 TextView。
    • 天哪,它有效。我早些时候试图这样做,但我认为不可能以这种方式做到这一点。非常感谢 :) 哦,这个糟糕的 GUI UI 编辑器:/
    • 是的,不要指望 GUI 编辑器。只需编辑 XML。如果这足够了,您可以接受它作为答案(点击我答案左侧的复选标记)。
    • 完成。我想现在我可以使用我写的代码更新这个图像视图,是吗?
    猜你喜欢
    • 2016-10-27
    • 2011-10-30
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2019-08-27
    • 2013-08-28
    • 2020-11-11
    • 2011-10-30
    相关资源
    最近更新 更多