自定义View的一些总结

1、自定义Viewandroid

一、构造方法的选中,获取一些须要用到的值app

二、重写onMeasure方法,计算子View的宽高,以及本身的宽高布局

三、重写onLayout方法,决定子View的布局位置this

四、须要用的手势是可重写onTouchEvent方法xml

2、构造方法test

一、一个参数的构造方法方法

context                             new CustomSidingView(context)项目

二、两个参数的构造方法
di

Context context, AttributeSet attrs(布局文件中申明的属性,在没有自定义属性的状况下)               CustomSidingView(Context context, AttributeSet attrs)文件

三、三个参数的构造方法

Context context, AttributeSet attrs, int defStyle(有自定义属性时并用自定义属性时调用)

CustomSidingView(Context context, AttributeSet attrs, int defStyle)


在自定义属性的时候通常三个构造方法都会用

在一个参数的构造方法调用                this(context, null);

在两个参数的构造方法中调用            this(context, attrs, 0);

在三个参数的构造方法中调用             super(context, attrs, defStyle);

3、自定义属性

一、在attrs.xml 文件中自定义

二、在布局文件中使用自定义属性(注意:在布局文件中必定要申明 xmlns  如:app="http://schemas.android.com/apk/res/com.test.sildingmenu"(其中“app”随便定义,“http://schemas.android.com/apk/res/”为固定格式,“com.test.sildingmenu”为项目包名))

三、在三个参数的构造方法里面得到自定义属性的值

相关文章
相关标签/搜索