android - 根据用户在配置 Activity 中的选择,在小部件中将 StateListDrawable 设置为 ImageButton/ImageView.backgroundDrawable

标签 android configuration widget imagebutton

我正在创建一个小部件,配置 Activity 中的用户可以在其中选择要在按钮(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>

我的问题是,如何根据用户在配置 Activity 中的选择,在小部件中显示的 ImageButton 中设置选定的 backgroundResource?

最佳答案

我会在这里回答我自己的问题。似乎无法通过代码在 RemoteViews 中设置 StateListDrawable。我的解决方案是使用引用唯一选择器的 XML 布局(我实际上制作了一个生成器,以便我可以轻松生成它们)。当处理大量图形元素时,这很乏味,但似乎是唯一的选择。

关于android - 根据用户在配置 Activity 中的选择,在小部件中将 StateListDrawable 设置为 ImageButton/ImageView.backgroundDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5127280/

相关文章:

android - AsyncTask 完成后在 ListView 上设置 OnItemClickListener

android - 用于我的 Android 应用程序的 Amazon alexa API

c# - 将 ConfigurationManager 重定向到另一个文件

php - Wordpress 自定义小部件记住多个选择选项

flutter - Flutter转换不同类型的图像类

android - 应用程序终止后,谷歌附近的 API 后台扫描不起作用

android - 设置 ListView 的最大可见行数

symfony - 使配置节点在 Symfony 2 配置中同时支持字符串和数组?

java - 使用 persistence.xml 创建 Hibernate SessionFactory

sql - 创建在 Azure Databricks 中传递日期以进行查询的小部件