Android 自定义组件(一) 基本实现方式和自定义属性

实现方式:

1. 继承自ViewGroup或Layout ,自定义设置子view的位置、尺寸等,用于组合一些组件,产生一个复合组件

2. 继承自已有的widget View,用于扩展现有组件的功能

3. 继承自View ,完全自定义一个组件

自定义类的构造函数:

	public CustomView2(Context context) {//直接在代码中调用时,使用该函数
		super(context);
	}
	
	public CustomView2(Context context, AttributeSet attrs) {//在xml中使用自定义view时,使用这个函数
		super(context, attrs);
	}

	public CustomView2(Context context, AttributeSet attrs, int defStyle) {//可以由上一个函数中手动调用
		super(context, attrs, defStyle);
	}
   自定义函数中的attrs表示view的属性集,defStyle表示默认的属性资源集的id

    在xml中使用自定义view的流程:

自定义属性

定义属性

res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>    
    <attr name="test" format="integer"/>
    <declare-styleable name="customview">
        <attr name="test"/> <!-- 复用声明在外部的属性定义test  -->
        
        <attr name="atr1" format="reference"/> <!-- 参考引用某个资源 如@drawable/img-->
        <attr name="atr2" format="string"/> <!-- 属性为string类型 -->
        <attr name="atr3" format="string|reference"/> <!--  string类型或引用 -->
        <attr name="atr4" format="boolean"/> <!-- 布尔型 true false  -->
        <attr name="atr5" format="integer"/> <!-- 整数 -->
        <attr name="atr6" format="float"/> <!-- 浮点  -->
        <attr name="atr7" format="color"/> <!-- 颜色值 #rgb  #rrggbb #argb #aarrggbb -->
        <attr name="atr8" format="dimension"/> <!-- 尺寸值 -->
        <attr name="atr9" format="fraction"/> <!-- 百分比 -->
        <attr name="atr10"> <!-- enum -->
            <enum name="spring" value="1"/>
            <enum name="summer" value="2"/>
        </attr>
        <attr name="art11"> <!-- 位或运算 表示 spring|summber  -->
            <flag name="spring" value="4"/>
            <flag name="summer" value="8"/>
        </attr>
    </declare-styleable>    
</resources>

布局中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/com.stone"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!--
	xmlns:android 默认的命名空间 表示所有以android开头的属性 都在 android包中找到
	xmlns:custom  在packagecom.stone包下定义的属性 即 <declare-styleable />中的所有属性
    -->

    <LinearLayout
        android:layout_width="150dp"
        android:layout_height="150dp" >
		
        <!-- 使用自定义view -->
        <com.stone.view.CustomView1
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            custom:score="60%"
            custom:rotation="-45"
            custom:color="#3f00ff00" />
        <!--
        	wrap_content, 那么宽度没有具体指定,对应测量模式规则 	MeasureSpec.AT_MOST
        	fill_parent			指定了高度	跟父view一样			MeasureSpec.EXACTLY
        -->
    </LinearLayout>
	
    <!-- 使用自定义view -->
    <com.stone.view.CustomView2
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
	
    <!-- 当自定义view是一个内部类时,需要像以下这样用<view 设置class属性 /> -->
    <view
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        class="com.stone.view.CustomView1$Custom1" />
    

</LinearLayout>

代码中解析自定义属性

	public CustomView1(Context context, AttributeSet attrs) {
		super(context, attrs);
		//atts 包括
		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.customview1);
		//系统会在自定义属性前加上它所属的declare-styleable 的name_
		int color = array.getColor(R.styleable.customview1_color, Color.WHITE);
		float rotation = array.getFloat(R.styleable.customview1_rotation, 0f);
		float score = array.getFraction(R.styleable.customview1_score, 0, 13, 10);
		array.recycle();//回收
		System.out.println("color=" + color + ", rotation=" + rotation + ", score=" + score);
		setBackgroundColor(color);
	}


Android 自定义组件(一) 基本实现方式和自定义属性,布布扣,bubuko.com

Android 自定义组件(一) 基本实现方式和自定义属性

上一篇:Android SharedPreferences复杂存储


下一篇:关于ios中的点赞控件效果的实现--UIControl