Android 自定义View

    xiaoxiao2021-12-14  15

    看到有很多自定义View 的但是我还是要写,我写博客就帮助自己记忆和的方便以后自己使用

    1自定义View的步骤:

    1、自定义View的属性

    2、在View的构造方法中获得我们自定义的属性

    3、重写onMesure 

    4、重写onDr

      

    Android在绘制View的时候必须对View进行测量,这个过程在onMeasure()中进行。

    测量模式有三种:

    EXACTLY 即精确模式当我们将控件的layout_width 属性或layout_height 属性指定具体的值时

    比如:android:layout_width="400dip" 或者指定为match_parent属性 时系统使用的是。

    AT_MOST:即最大值模式,当控件的layout_width属性或layout_height 属性指定为wrap_content

    时控件大小一般随着控件的子控件或者内容变化而变化,此时控件的尺寸只要不要超过父控件允许的最大尺寸即可。

    UNSPECIFIED:这个属性比较奇怪---它不指定其大小测量模式,view 想多大就多大多在自定义view的时候才会使用。

    废话少说上实例:

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec),measureWidth(heightMeasureSpec)); } private int measureWidth(int measureSpec){ int result =0 ; int specMode = MeasureSpec.getMode(measureSpec); int specSide = MeasureSpec.getSize(measureSpec); if(specMode==MeasureSpec.EXACTLY){ result= specSide; }else { result = 200; if(specMode==MeasureSpec.AT_MOST){ result = Math.min(result,specSide); } } return result ; }

    转载请注明原文地址: https://ju.6miu.com/read-962444.html

    最新回复(0)