zoukankan      html  css  js  c++  java
  • Android开发学习笔记-自定义组合控件的过程

    自定义组合控件的过程

    1.自定义一个View 一般来说,继承相对布局,或者线性布局 ViewGroup;
    2.实现父类的构造方法。一般来说,需要在构造方法里初始化自定义的布局文件;
    3.根据一些需要或者需求,定义一些API方法;

    ----------------------------------
    4.根据需要,自定义控件的属性,可以参照TextView属性;

    5.自定义命名空间,例如:
    xmlns:itheima="http://schemas.android.com/apk/res/《包名》"
    xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.mobilesafe"

    6.自定义我们的属性,在Res/values/attrs.xml

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

    <declare-styleable name="TextView">
    <attr name="title" format="string" />
    <attr name="desc_on" format="string" />
    <attr name="desc_off" format="string" />
    </declare-styleable>

    </resources>

    7.使用我们自定义的属性
    例如:
    itheima:title="设置自动更新"
    itheima:desc_on="设置自动更新已经开启"
    itheima:desc_off="设置自动更新已经关闭"

    8.在我们自定义控件的带有两个参数的构造方法里AttributeSet attrs 取出我们的属性值,关联自定义布局文件对应的控件;

  • 相关阅读:
    Java方法
    Java程序控制
    Java基础语法
    编程语言的两大类型
    IntelliJ IDEA优化配置
    Hello World
    Java安装及卸载
    Java来龙去脉
    计算机底层基础
    Day14
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/4014722.html
Copyright © 2011-2022 走看看