1.自定义View 一般继承相对布局或相对布局,必须是ViewGroup
2.实现父类的构造方法,一般需要在构造方法中,初始化自定义的布局文件
3.根据需求,定义方法,设置XXX
4.自定义属性
5.自定义命名空间
<Layout xmls:(自定义)=“http://schemas.android.com/apk/res/(包名)”
<Layout xmlns:aze="http://schemas.android.com/apk/res-auto"
参考 android-sdk-macosx ▸ platforms ▸ android-16 ▸ data ▸ res ▸ values>attrs.xml
6.自定义属性,在res/values/attrs.xml
<declare-styleable name="RelativeLayout">
<attr name="gravity" />
<!-- Indicates what view should not be affected by gravity. -->
<attr name="ignoreGravity" format="reference" />
</declare-styleable>
7.使用自定义的属性
<com.bruce.mobilesafe.ui.SettingItemView
aze:title="设置自动更新"
aze:desc_on="自动更新已经开启"
aze:desc_off="自动更新已经关闭"
android:id="@+id/siv_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.bruce.mobilesafe.ui.SettingItemView>
8.设置属性
在布局文件中设置这个类 使用的是带有两个参数的构造方法
布局文件中的属性,被封装成为AttributeSet
带有3个参数的构造方法,有默认样式
一个参数的构造方法,,new
attrs.getAttritubeValue(namespace,name);