我有一个带有样式选择的微调器(我在 strings.xml
中设置了微调器样式),如果我在 main.xml
中设置样式,效果很好。但我想知道如何以编程方式设置样式,其中样式已在 strings.xml
中定义。
请引用下面的代码
main.xml:
<Spinner
android:id="@+id/PassengersSpinner1"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_span="3"
android:layout_marginTop="6dp"
style="@style/SpinnerStyle" />
strings.xml:
<style
name="SpinnerStyle"
parent="@android:style/Widget.Spinner">
<item name="android:background">@android:drawable/btn_default</item>
</style>
现在,我想知道如何在没有 main.xml
的情况下以编程方式设置此 strings.xml
?
最佳答案
根据我的阅读,您不能这样做,因为必须在创建 View 之前设置样式。
如果您在代码中创建 View ,您可以做到这一点(因为这样您就可以在创建 View 之前设置样式)。
如果您想这样做,here 就是一个很好的例子。 。问题和答案都为您提供了实现目标的方法。
关于android - 如何以编程方式设置微调器的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11482742/