Android 自定义属性,系统控件扩展
来源:互联网 发布:mysql install db 编辑:程序博客网 时间:2024/04/29 05:00
Android 可以自定义View,同时我们也可以为我们的自定义的View添加自定义属性,对系统的控件实现扩展,使用方式如同系统控件在xml布局文件中的使用形式。
扩展方式:自定义属性,然后再布局文件中使用这些属性,在自定义View中获取这些自定义属性的值。
具体方式如下:
1.定义属性:在res/values目录下创建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RoundAngleImageView"> <attr name="roundWidth" format="dimension" /> <attr name="roundHeight" format="dimension" /> </declare-styleable> </resources>
其中name为该属性集的名字,在第三部获取属性值时
roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);需要在每个参数名前面添加这个name和下划线。
属性的类型即format有string , integer , dimension , reference , color , enum.,如果可以同时使用不同的类型。用”|“分割。
枚举类型的设置略有不同
<attr name="flowDirection" format="enum"> <enum name="leftToRight" value="0" /> <enum name="topDown" value="1" /> <enum name="rightToLeft" value="2" /> <enum name="bottomUp" value="3" /> </attr>
2.在布局文件中使用属性
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.liupan.app.RoundImage android:id="@+id/roundImage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@drawable/control_bar" android:gravity="center" app:roundWidth = "5dp" app:roundHeight="10dp"/> </RelativeLayout>
首先需要
xmlns:app="http://schemas.android.com/apk/res-auto"
3. 在自定义组件中的构造函数中获取设置的属性值
public RoundCornerImageView(Context context) { super(context); init(context, null); } public RoundCornerImageView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public RoundCornerImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); }
第一个构造函数没有AttributeSet参数,我们需要设置默认值。
在获取属性值得时候,需要设置默认值,在用户没有设置本属性时,使用默认值。
private void init(Context context, AttributeSet attributeSet) { if (attributeSet != null) { TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.RoundAngleImageView); assert a != null; roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth); roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight); } else { float density = context.getResources().getDisplayMetrics().density; roundWidth = (int) (roundWidth * density); roundHeight = (int) (roundHeight * density); } paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); paint2 = new Paint(); paint2.setXfermode(null); a.recycle();//为了保持以后使用的一致性,需要回收
注意最后调用TypedArray的recycle方法。
0 0
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- Android 自定义属性,系统控件扩展
- Android读取系统控件的自定义属性
- android 自定义控件属性
- Android自定义控件属性
- android 自定义控件属性
- Android自定义控件属性
- Android自定义控件属性
- Android自定义控件属性
- Android自定义控件属性
- Android控件:自定义属性
- Android自定义控件属性
- android 自定义控件属性
- Android 自定义控件属性
- 存在着才有机会创造奇迹
- 事件委托
- 关于通过水质参数推测得出水质变化原因方法的一点心得
- python 简单业务爬虫
- SQL语句里的limit使用方法
- Android 自定义属性,系统控件扩展
- 根据手机ip地址获取城市,避免申请地图定位, String .split("\\s+")
- MyBatis+MySQL 返回插入的主键ID
- Git fetch和git pull的区别
- vue进行图片的预加载watch用法
- 双端队列 C 实现
- System.Data.OracleClient 与Oracle.ManagedDataAccess.Client
- A/B----乘法逆元
- java猜数游戏