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

实现方式:

1. 继承自View ,自定义一个View. 

2. 继承自ViewGroup(容器) ,自定义设置子view的位置、尺寸等,用于组合一些组件,产生一个复合组件 

3. 继承自已有的组件(View型 或 ViewGroup型),用于扩展现有组件的功能

 

自定义类的构造函数:

 

	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);
	}
 public CustomView2(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {//可以由上两个函数中调用 5.0后新增
                super(context, attrs, defStyleAttr, defStyleRes);
        }

  > 自定义函数中的attrs表示view的属性集(res/layout/下xml中定义的 所有属性含自定义属性)。 

  >  defStyleAttr 默认属性集的Style. 可以传入当前Theme中含有的一个style;

          如果传入0或不存在于当前Theme中的style,则不被应用

  >  defStyleRes 表示一个默认Style的资源.  可以是自己定义的,也可以是系统的

若定义了相同的属性, 那么被应用的优先级:  attrs > defStyleAttr > defStyleRes;否则,都将被应用

 

 

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

 

注意枚举属性值,必须是 整形Integer类型。

注意 <attr>的声明, 若在不同的  <declare-styleable> 中声明了相同名字的 <attr> 编译会报错。这时,可以考虑使用声明在外部,从而内部复用。

 

布局中使用

<?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);
	}

R.styleable.customeview在R文件中就是一个int数组

 

在style中使用自定义属性

定义style如:

   <style name="togglebuttonStyle">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_gravity">center</item>
        <item name="com.stone.togglebutton:isOpened">false</item>
    </style>

<item> 中的 name 使用的就是内置的属性或自定义的属性,  即 <attr>所定义的。

在布局xml中,对某个view标签内使用 style="@style/tollgebuttonStyle" 。需要在view的实现中,解析相关属性。

 

?attr/

由 <attr> 定义属性。

<style> 中 <item>来使用,并设置值。

manifest中的 <application> 或<activity>来设置theme主题,android:theme="@style/..."   。

最后,在布局xml中,view标签内,就可以使用 ?attr/某个attr-name  来引用 主题style内定义的 相应属性值

 

综上

1. <attr> 定义属性,name指定名称,format指定格式,还有枚举、或运算格式。

2. 直接在布局view标签内,通过命名空间来引用属性,并给定一个符合格式的值,然后在view中通过TypedArray来解析。

3. 或,在<style> 中 设定 <item> 来引用 attr,并给定值。通过theme主题的应用,对相关属性设定默认值。布局view标签中,使用?attr/... 来引用。


注意自定义属性的命名空间:

eclipse 下 需要使用app的package:xmlns:custom="http://schemas.android.com/apk/res/com.stone"

android studio 下:xmlns:custom="http://schemas.android.com/apk/res-auto"

现在,一般使用 xmlns:app 的命名空间

 

示例地址 https://github.com/aa86799/MyCustomView/tree/master/custom_attr_style_res

 


下一节:

Android 自定义组件(二) 如何实现自定义组件   http://blog.csdn.net/jjwwmlp456/article/details/41076699

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值