我想动态更改按钮的样式,即在 Java 代码中,类似于:
((Button)findViewById(id)).setStyle("@styles/foo")
<resources>
<style name="foo">
<item name="android:adjustViewBounds">true</item>
<item name="android:maxHeight">100px</item>
<item name="android:maxWidth">200px</item>
</style>
</resources>
我还没有看到像 setStyle 这样的东西,所以:
我必须更改每个属性还是可以更改整个样式?
像这样分配样式
<style name="ButtonHOLO" parent="android:Widget.Button">
<item name="android:background">@drawable/btn_default_holo_dark</item>
<item name="android:minHeight">@dimen/calc_btn_h</item>
<item name="android:minWidth">@dimen/calc_btn_w</item>
<item name="android:textColor">#ffffff</item>
</style>
对于一个动态按钮,您需要同时使用 setBackgroundResource() 和 setTextAppearance() 函数。例如:
btn.setBackgroundResource(R.drawable.btn_default_holo_dark);
btn.setTextAppearance(context, R.style.ButtonHOLO);
在哪里
btn_default_holo_dark
是描述按钮选择器的 .xml 文件的名称。