您的位置:首页 > 移动开发 > Android开发

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

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

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

具体方式如下:

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

[java] view
plaincopy





<?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.,如果可以同时使用不同的类型。用”|“分割。

枚举类型的设置略有不同

[java] view
plaincopy





<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.在布局文件中使用属性

[java] view
plaincopy





<?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>  

首先需要

[java] view
plaincopy





xmlns:app="http://schemas.android.com/apk/res-auto"   

xmlns后边的app这个名字可以任意取,属性设置就是使用这个名字开头。

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

[java] view
plaincopy





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参数,我们需要设置默认值。

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

[java] view
plaincopy





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方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: