我有几个自定义的View
,其中我创建了在xml布局中声明并在视图构造函数期间读取的自定义样式属性。我的问题是,如果我没有为定义xml布局时的所有自定义属性提供显式值,如何使用样式和主题来传递默认值到我的View
构造函数中?
例如:
attrs.xml:
<declare-styleable name="MyCustomView">
<attr name="customAttribute" format="float" />
</declare-styleable>
layout.xml(android:
标签仅为简化起见而省略):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mypackage" >
<-- Custom attribute defined, get 0.2 passed to constructor -->
<com.mypackage.MyCustomView
app:customAttribute="0.2" />
<-- Custom attribute not defined, get a default (say 0.4) passed to constructor -->
<com.mypackage.MyCustomView />
</LinearLayout>