自定义组合控件
建立一个类继承某种layout,有三个构造函数,mymulview(Context context),代码创建组件时候调用
mymulview(Context context, AttributeSet attrs),用xml创建组建时候调用。 AttributeSet attrs,包含xml中定义的各项属性值。
在重写这个构造时候,要实例化一个view组合(通过inflate一个xml布局文件)。这个实例化的view就是自定义view。
自定义属性,要在values中建立xml文件。这个是用来在xml布局中,定义属性值,在通过xml构造组件时候mymulview(Context context, AttributeSet attrs),attrs包含属性值集合
构造函数中,提取其中包含的属性值,然后给控件设置
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name = "myview">
<attr name = "mytext" format = "string" />
</declare-styleable>
</resources>
xml布局文件中,名字空间声明。 xmlns:test="http://schemas.android.com/apk/res/com.example.myview"
test的名称可以随便设置,用在自定义属性的前面,后面不能随便http://schemas.android.com/apk/res/+所在的报名
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.example.myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.myview.mymulview
android:id="@+id/myview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
test:mytext="zhedouxing" >
</com.example.myview.mymulview>
</LinearLayout>