Dar*_*man 13 android coding-style button
我想动态地改变按钮的样式,即在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>
Run Code Online (Sandbox Code Playgroud)
我没有看到像setStyle这样的东西,所以:
我是否必须改变每一个属性或者我可以改变整个风格?
小智 19
分配这样的样式
<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>
Run Code Online (Sandbox Code Playgroud)
动态地按钮,你需要使用setBackgroundResource()和setTextAppearance()函数.例如:
btn.setBackgroundResource(R.drawable.btn_default_holo_dark);
btn.setTextAppearance(context, R.style.ButtonHOLO);
Run Code Online (Sandbox Code Playgroud)
哪里
btn_default_holo_dark
是.xml文件的名称,它描述了按钮的选择器.