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

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

2014-08-21 16:42 656 查看

实现方式:

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>


布局中使用

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

注意自定义属性的命名空间:
eclipse
下 需要使用app的package:xmlns:custom="http://schemas.android.com/apk/res/com.stone"

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

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

下一节:
Android 自定义组件(二) 如何实现自定义组件   http://blog.csdn.net/jjwwmlp456/article/details/41076699
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: