自定义View之构造方法和用法
来源:互联网 发布:红帽linux 10天就重启 编辑:程序博客网 时间:2024/04/30 16:04
自定义view三个构造方法:
public SwipeRecycleView(Context context) { this(context,null); } public SwipeRecycleView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public SwipeRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
- 在代码中直接new一个Custom View实例的时候,会调用第一个构造函数.这个没有任何争议.
- 在xml布局文件中调用Custom View的时候,会调用第二个构造函数.这个也没有争议.
- 在xml布局文件中调用Custom View,并且Custom View标签中还有自定义属性时,这里调用的还是第二个构造函数.
第一步:Custom View添加自定义属性主要是通过declare-styleable标签为其配置自定义属性,具体做法是: 在res/values/目录下增加一个resources xml文件,示例如下(res/values/attrs.xml):其他也可以,一般定义成attrs
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="SwipeMenuLayout"> <attr name="right_menu_id" format="reference"/> </declare-styleable></resources>第二步:
在设置自定义属性之前,我们首先要在主Activity的布局文件中调用我们的Custom View,并且为其设置特定的属性.
<com.android.lyf.recycle.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="70dp" android:id="@+id/swipe_menu" android:background="@color/white" android:orientation="horizontal" app:right_menu_id="@+id/ll_right_menu" >注意:在给自定义属性赋值时,首先需要增加自定义属性的命名空间,例如: xmlns:app=”http://schemas.Android.com/apk/res-auto”,android Studio推荐使用res-auto,在Eclipse中需要使用Custom View所在的包名: xmlns:app=”http://schemas.android.com/apk/com.kevintan.eventbussample.view”
第三步:
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwipeMenuLayout);mRightId = typedArray.getResourceId(R.styleable.SwipeMenuLayout_right_menu_id, 0);typedArray.recycle();我是为了得到左边menu的宽度
@Overrideprotected void onFinishInflate() { super.onFinishInflate(); if (mRightId!=0){ rightMenuView = findViewById(mRightId); }}***************************************************************************************************************************
阅读全文
0 0
- 自定义View之构造方法和用法
- 自定义view之构造方法
- 自定义view-构造方法和自定义属性
- 自定义view构造方法
- 自定义View的构造方法
- 自定义view的构造方法
- 自定义view的构造方法和样式主题
- 自定义View构造方法的推荐写法
- 自定义View的三个构造方法
- 关于自定义View的四个构造方法
- 构造方法之自定义构造方法
- 自定义View起步:自定义View分类和构造函数
- 自定义View之构造函数学习
- Android自定义View 之构造函数
- 自定义View之onMeasure方法
- 自定义View之onMeasure()方法
- 安卓自定义view之Canvas和Paint方法
- android自定义View构造方法以及获取自定义属性详解
- Mybatis使用generator自动生成映射配置文件信息
- Prime 素数判断
- 0-1背包问题
- 为什么在python里推荐使用多进程而不是多线程
- MySQL双主一致性架构优化(58沈剑)
- 自定义View之构造方法和用法
- Java线程池类ThreadPoolExecutor、ScheduledThreadPoolExecutor及Executors工厂类
- 我的个人博客终于开张了
- matlab 将变量代入solve里面怎么解?利用循环后又怎么解?
- 深度学习中的卷积与反卷积
- 小程序wx.uploadFile出现中文无法上传或者出现乱码
- USACO-Section2.1 Healthy Holsteins [搜索][枚举]
- 最后剩下的一张骨牌的编号
- Failed to start firewalld.service: Unit firewalld.service is masked