zoukankan      html  css  js  c++  java
  • 自定义控件

    为什么会有自定义控件?
    自定义控件实现常用有几种方式?
    我们运行一段Android代码,如果用Android本身的控件,在HUAWEI手机上和在小米手机上它的效果可能会不一样
    系统本身的控件不足以满足我们的开发需求,就像一个textview控件我们想让它显示出艺术字的效果,系统原生控件不能满足我们。
    这样就出现了自定义控件。

    自定义控件实现的方式

    1.继承系统原生控件(一般是继承TextView,Button,ImageView等)

    自定义属性:我们可以给Button指定显示的颜色等
    自定义属性步骤:
    ①在res的values目录下新建attr.xml文件。
    attr文件样例:

    <declare-styleable name="CustomView">
     <attr name="editTextColor" format="reference|color" />
        <attr name="editTextStyle" format="reference" />
        <attr name="epyView" format="reference" />
        <attr name="epyVisibility">
            <enum name="gone" value="0" />
            <enum name="invisible" value="1" />
        </attr>
        <attr name="layout_constraintHeight" format="dimension">
            <enum name="wrap" value="-2" />
        </attr>
    </declare-styleable>
    

    上面我们是自定义属性的常用定义方式,一般就这几种。
    ②在xml布局文件中使用自定义属性。

    app:editTextColor="#fff"
    app:editTextStyle="引用的style"
    

    ③在定义的View中获取自定义属性的引用。

    TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.CustomView);
    

    2.继承View

    系统中的每个控件大都逃脱不了这个命运,经历出生的三大阶段onMeasure(),onLayout()和onDraw()。(像button,TextView等也都经历了这三大阶段)
    onMeasure()是测量视图的大小。
    onLayout()是给视图进行布局。(测量好控件的大小之后用这个方法确定把它放到哪个地方)
    onDraw()对视图进行绘制。(确定位置后把控件画出来)

    3.继承ViewGroup(当然也包括ViewGroup的子类LinearLayout,RelativeLayout等)

    viewGroup要指定里面各个控件的位置。
    样例见我的github地址:https://github.com/WindChenx/CustomControl

  • 相关阅读:
    用Springboot写一个只有一个条件的复杂查询
    Springboot The requested profile "pom.xml" could not be activated because it doesn't not exists
    springboot配置路径
    vuex
    @MappedSuperclass的作用
    icon.css
    default.html
    WebService调用
    通用分页存储过程
    存储过程获得最新订单号
  • 原文地址:https://www.cnblogs.com/dearnotes/p/11953381.html
Copyright © 2011-2022 走看看