Android:自定义视图的默认属性 [英] Android: Default attributes for custom views
问题描述
我有一个扩展框架类之一的自定义视图. Android中大多数View
都有一些为其定义的默认属性(例如Button
是可单击的,由android:clickable="true"
设置).
I have a custom view that extends one of the framework classes. Most View
s in Android have some default attributes defined for them (such as Button
being clickable, which is set by android:clickable="true"
).
如何为自定义视图提供应用程序范围的默认值?
How do I provide application-wide defaults for my custom view?
推荐答案
我因此解决了自己的问题:
I solved my own problem thusly:
res/values/attrs.xml :
<resources>
<attr name="customViewStyle" type="reference" />
</resources>
res/values/styles.xml :
<resources>
<style name="AppTheme" parent="@android:style/Theme.Light">
<item name="android:background">@drawable/bg_light</item>
<item name="customViewStyle">@style/CustomView</item>
</style>
<style name="CustomView">
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
</style>
</resources>
然后,您要做的就是将清单中的主题设置为AppTheme.这也适用于使用android:[widget]Style
代替AppTheme定义中的customViewStyle
的标准小部件.
Then all you do is set the theme in the manifest to AppTheme. This also works with standard widgets using android:[widget]Style
in place of customViewStyle
in the definition of AppTheme.
这篇关于Android:自定义视图的默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!