我有一个自定义视图,扩展了其中一个框架类。 Android中的大多数View
都为其定义了一些默认属性(例如Button
可点击,由android:clickable="true"
设置)。
如何为自定义视图提供应用程序范围的默认设置?
答案 0 :(得分:12)
我解决了自己的问题:
<强> RES /值/ attrs.xml 强>:
<resources>
<attr name="customViewStyle" type="reference" />
</resources>
<强> RES /值/ 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。这也适用于在AppTheme定义中使用android:[widget]Style
代替customViewStyle
的标准小部件。
答案 1 :(得分:1)
您还可以为此布局创建样式,并设置使用该视图的样式。
来源:http://developer.android.com/guide/topics/resources/style-resource.html
答案 2 :(得分:0)
我认为您需要覆盖带有AttributeSet参数的View构造函数,并构建您自己的修改后的AttributeSet以传递给超类构造函数。
编辑:更简单,只需让你的View子类构造函数调用setClickable
等将默认值更改为你想要的。