自定义组合控件和属性

来源:互联网 发布:淘宝店铺简介如何写 编辑:程序博客网 时间:2024/06/08 08:07
自定义组合控件的过程:
1.自定义一个View 一般来说,继承相对布局,或者线性布局  ViewGroup;
2.实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件;
3.根据一些需要或者需求,定义一些API方法;
4.根据需要,自定义控件的属性,可以参照TextView属性;
5.自定义命名空间,例如:
  xmlns:前缀="http://schemas.android.com/apk/res/《包名》"
  xmlns:myAttribute="http://schemas.android.com/apk/res/com.cjh.test"
6.自定义我们的属性,在Res/values/attrs.xml(参考sdk\platforms\android-14\data\res\values\attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextView">
        <attr name="name" format="string" />
    </declare-styleable>

</resources>

7.使用我们自定义的属性
 例如:
        myAttribute:name="我的名字"
8.在我们自定义控件类的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义布局文

件对应的控件;


0 0