• 自定义组合控件的过程


    自定义组合控件的过程

    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 取出我们的属性值,关联自定义布局文件对应的控件;

  • 相关阅读:
    设计模式(二)装饰器模式
    AppManager
    判断用户是否已经登录
    RecyclerView 下拉刷新和加载更多
    Android Application中的Context和Activity中的Context的异同
    Android中通过反射获取资源Id
    Android解析WindowManager(一)WindowManager体系
    设计模式(二)模板方法模式
    staticmethod
    反射
  • 原文地址:https://www.cnblogs.com/mukekeheart/p/5783025.html
Copyright © 2020-2023  润新知