zoukankan      html  css  js  c++  java
  • 最简单的自定义控件实现

    在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

  • 相关阅读:
    【网络游戏同步技术】帧同步的一致性
    【C++】STL常用容器总结之五:双端队列deque
    使 egg-multipart 同时支持 stream 和 file
    react + 图灵api 实现模拟客服
    egg 扩展上传文件白名单
    input[type=file] 样式美化,input上传按钮美化
    react-lazyload 实现图片懒加载
    useEffect 模拟 react 生命周期
    egg 实现下载数据,并保存成excel文件
    egg 实现上传excel,并解析保存到数据库
  • 原文地址:https://www.cnblogs.com/starwolf/p/3736412.html
Copyright © 2011-2022 走看看