最简单的自定义控件实现

在Android中实现自定义控件只需要三步。

第一步,写自定义控件的布局文件

mykjj.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/scrollView">
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <Button
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Button"
                    android:id="@+id/button"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="New Button"
                    android:id="@+id/button2"/>
            <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:text="New CheckBox"
                    android:id="@+id/checkBox"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

第二步,写自定义控件的实现类,注意最简单的实现方式就是初始化三个构造函数,并在初始化函数中通过layoutInflater 实现 对xml文件的映射。

package com.example.zidingyi_kongjian;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

/**
 * Created by britzlieg on 14-5-19.
 */
//自定义控件
public class mykj extends LinearLayout{
    //一定要有以下的三个构造函数
    public mykj(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public mykj(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public mykj(Context context) {
        super(context);
        init();
    }
    //初始化函数
    private void init(){
        //主要是将XML映射到java文件中,相当于找layout下的xml布局文件,与findviewbyid找widget是一样的原理
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater layoutInflater;
        layoutInflater = (LayoutInflater)getContext().getSystemService(infService);
        layoutInflater.inflate(R.layout.mykjj,this,true);
    }
}

第三步,在主布局文件中加入自定义控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />
<com.example.zidingyi_kongjian.mykj android:layout_width="fill_parent"
                                    android:layout_height="fill_parent">

                                    </com.example.zidingyi_kongjian.mykj>
</LinearLayout>

总结:

其实这三步已经可以实现最简单的自定义控件,其他更复杂的自定义控件的实现原理也与这个差不多。

有兴趣可以参考一下这篇博文:http://www.cnblogs.com/Greenwood/archive/2011/03/02/1969325.html

posted @ 2014-05-19 10:59  月之星狼  阅读(277)  评论(0编辑  收藏  举报