根据用户在配置活动中的选择,在小部件中将 StateListDrawable 设置为 ImageButton/ImageView.backgroundDrawable
我正在创建一个小部件,其中配置活动中的用户可以从要在按钮中使用的各种图形中进行选择(ImageView(或 ImageButton)和 StateListDrawable)。添加小部件时,所选图形将在小部件布局中使用。布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/button"
android:layout_width="72dip"
android:layout_height="72dip"
android:background="@null"
android:clickable="true">
</ImageView>
</LinearLayout>
我的问题是,如何根据用户在配置活动中的选择来设置小部件中显示的 ImageButton 中选定的backgroundResource?
I'm creating a widget where the user in the configuration activity can select from a variety of graphics to be used in a button (ImageView (or ImageButton) with StateListDrawable). The selected graphic will be used in the widget layout when the widget is added. The layout is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:id="@+id/button"
android:layout_width="72dip"
android:layout_height="72dip"
android:background="@null"
android:clickable="true">
</ImageView>
</LinearLayout>
My question is, how do I set the selected backgroundResource in the ImageButton shown in the widget based on the users choice in the configuration activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在这里回答我自己的问题。似乎无法通过代码在 RemoteViews 中设置 StateListDrawable 。我的解决方案是使用 XML 布局,它引用了唯一的选择器(我实际上制作了一个生成器,以便我可以轻松生成它们)。当处理大量图形元素时,这很乏味,但似乎是唯一的选择。
I'll answer my own question here. It seems like it's not possible to set a StateListDrawable in RemoteViews via code. The solution for me was to use XML-layouts which has references to unique selectors (I actually made a generator, so that I could generate them easily). This is tedious when working with a large number of graphic elements, but seems to be the only option.