android - 以编程方式更改 Spinner 背景

标签 android xml android-spinner

我有一些微调器需要更改背景。所以,我有这个 xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/iconodesplegableactivar"
 android:state_enabled="true"/>
<item android:drawable="@drawable/iconodesplegable"
android:state_enabled="false" android:state_window_focused="false"/>
</selector>

我在布局文件中设置的 Spinners 没有问题,但有些 Spinners 是动态生成的。

我如何在动态生成的微调器中使用该 xml? setBackground() 方法不允许您将 xml 设置为参数,而且我无法向 XML 添加 ID。

谢谢!

最佳答案

取自this answer ,以及我也在使用的东西:

如果您要动态创建 Spinner 则使用此:

// to change background of the popup list
  spinner.setPopupBackgroundResource(R.drawable.spinner_background);

// to change the `Spinner` background
  spinner.setBackgroundResource(R.drawable.your_drawable);

这里是 spinner_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>

关于android - 以编程方式更改 Spinner 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32139580/

相关文章:

java - android SQLite 方法故障(?!?!)

java - 通过单击另一个 Activity 中的按钮将项目添加到一个 Activity 中的 fragment 中的 ListView

java - 检查当前使用哪个 ArrayAdapter Spinner

android - 在 Kotlin 中测试私有(private)方法

c# - LINQ xml 查找节点返回 null

c# - XML 序列化 : System. InvalidOperationException:<tagname> 不是预期的

java - 如何在java中使用Prepared Statement进行Microsoft SQL Xpath查询

java - 如何更改微调器的文本颜色

ANDROID 微调器不显示最后选择的项目

java - android:layout_weight 考虑 FrameLayout 中嵌入 ImageViewer 的大小