在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

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. NPM 和webpack 的基础使用
  3. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  4. python list.sort()根据多个关键字排序的方法实现
  5. android源码编译出现No private recovery resources for TARGET_
  6. 【Android】Android中选项菜单(OptionMenu)的创建
  7. 用layer-list一次加载多个层次图片
  8. Android实现图片帮助跳转以及选择重拍Sqlite本地保存
  9. 实现Android屏幕控制Androidscreencast应用

随机推荐

  1. Android studio 入门笔记
  2. Android OOBE开发
  3. 【Android】动态注册广播接收器
  4. .net平台借助第三方推送服务在推送Androi
  5. React Native Android白屏优化终极方案
  6. Android中发送短信等普通方法
  7. 直接拿来用!最火的40个Android开源项目
  8. Android 开发之旅:深入分析布局文件&又是
  9. android中类实现Serializable,Parcelable
  10. Android网络编程之通过Get方法实现