Android 自定义属性,系统控件扩展

Android 可以自定义View,同时我们也可以为我们的自定义的View添加自定义属性,对系统的控件实现扩展,使用方式如同系统控件在xml布局文件中的使用形式。


扩展方式:自定义属性,然后再布局文件中使用这些属性,在自定义View中获取这些自定义属性的值。


具体方式如下:

1.定义属性:在res/values目录下创建attrs.xml文件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="RoundAngleImageView">  
  4.         <attr name="roundWidth" format="dimension" />  
  5.         <attr name="roundHeight" format="dimension" />  
  6.     </declare-styleable>  
  7. </resources>  

其中name为该属性集的名字,在第三部获取属性值时

roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);需要在每个参数名前面添加这个name和下划线。

属性的类型即format有string , integer , dimension , reference , color , enum.,如果可以同时使用不同的类型。用”|“分割。

枚举类型的设置略有不同

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <attr name="flowDirection" format="enum">  
  2.            <enum name="leftToRight" value="0" />  
  3.            <enum name="topDown" value="1" />  
  4.            <enum name="rightToLeft" value="2" />  
  5.            <enum name="bottomUp" value="3" />  
  6.        </attr>  


2.在布局文件中使用属性
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"   
  4.     android:orientation="vertical"   
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="fill_parent"   
  7.     >   
  8.     <com.liupan.app.RoundImage android:id="@+id/roundImage"   
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:layout_alignParentBottom="true"   
  12.         android:background="@drawable/control_bar"  
  13.         android:gravity="center"   
  14.         app:roundWidth = "5dp"  
  15.         app:roundHeight="10dp"/>   
  16. </RelativeLayout>  
首先需要

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. xmlns:app="http://schemas.android.com/apk/res-auto"   
xmlns后边的app这个名字可以任意取,属性设置就是使用这个名字开头。



3. 在自定义组件中的构造函数中获取设置的属性值

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public RoundCornerImageView(Context context) {  
  2.       super(context);  
  3.       init(context, null);  
  4.   }  
  5.   
  6.   public RoundCornerImageView(Context context, AttributeSet attrs) {  
  7.       super(context, attrs);  
  8.       init(context, attrs);  
  9.   }  
  10.   
  11.   public RoundCornerImageView(Context context, AttributeSet attrs, int defStyle) {  
  12.       super(context, attrs, defStyle);  
  13.       init(context, attrs);  
  14.   }  

第一个构造函数没有AttributeSet参数,我们需要设置默认值。

在获取属性值得时候,需要设置默认值,在用户没有设置本属性时,使用默认值。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. private void init(Context context, AttributeSet attributeSet) {  
  2.        if (attributeSet != null) {  
  3.            TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.RoundAngleImageView);  
  4.            assert a != null;  
  5.            roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);  
  6.            roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);  
  7.        } else {  
  8.            float density = context.getResources().getDisplayMetrics().density;  
  9.            roundWidth = (int) (roundWidth * density);  
  10.            roundHeight = (int) (roundHeight * density);  
  11.        }  
  12.   
  13.        paint = new Paint();  
  14.        paint.setColor(Color.WHITE);  
  15.        paint.setAntiAlias(true);  
  16.        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));  
  17.   
  18.        paint2 = new Paint();  
  19.        paint2.setXfermode(null);  
  20.       a.recycle();//为了保持以后使用的一致性,需要回收  


注意最后调用TypedArray的recycle方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值