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

Android自定义控件

2015-10-12 00:00 531 查看
摘要: 本文主要是介绍Android自定义组件的自定义属性方面的内容,为有兴趣的同学提供参考。

如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的应用所在的包路径",如果是最新的ADT插件可以使用
xmlns:前缀=
"http://schemas.android.com/apk/res-auto"。例如xmlns:my=
"http://schemas.android.com/apk/res-auto",在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。


例子:

/**
* 这个是自定义的TextView.
* 至少需要重载构造方法和onDraw方法
* 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
* 如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
* 并根据需要设定默认值,放在在xml文件中没有定义。
* 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
* 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
* 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们应用所在的包
* @author Administrator
*
*/
public class MyView extends View {

Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息
public MyView(Context context) {
super(context);

}

public MyView(Context context, AttributeSet attrs){
super(context, attrs);
mPaint = new Paint();
//TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
//在使用完成后,一定要调用recycle方法
//属性的名称是styleable中的名称+“_”+属性名称
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定
float textSize = array.getDimension(R.styleable.MyView_textSize, 36);
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);

array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
}

public void onDraw(Canvas canvas){
super.onDraw(canvas);
//Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形
//mPaint = new Paint();
//mPaint.setColor(Color.RED);
mPaint.setStyle(Style.FILL); //设置填充
canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形

mPaint.setColor(Color.BLUE);
canvas.drawText("我是被画出来的", 10, 120, mPaint);
}
}

对应属性文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
</resources>

在布局文件中的使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/com.wuke.view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<com.wuke.view.MyView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
my:textColor="#FFFFFFFF"
my:textSize="22dp"/>
</LinearLayout>

自定义属性步骤:

一、在res/values文件下定义一个attrs.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<declare-styleable name="ToolBar">

<attr name="buttonNum" format="integer"/>

<attr name="itemBackground" format="reference|color"/>

</declare-styleable>

</resources>

二、在布局xml中如下使用该属性:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:toolbar="http://schemas.android.com/apk/res/cn.zzm.toolbar"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<cn.zzm.toolbar.ToolBar android:id="@+id/gridview_toolbar"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:background="@drawable/control_bar"

android:gravity="center"

toolbar:buttonNum="5"

toolbar:itemBackground="@drawable/control_bar_item_bg"/>

</RelativeLayout>

三、在自定义组件中,可以如下获得xml中定义的值:

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

buttonNum = a.getInt(R.styleable.ToolBar_buttonNum, 5);

itemBg = a.getResourceId(R.styleable.ToolBar_itemBackground, -1);

a.recycle();

这么简单的三步,即可完成对自定义属性的使用。

*********************************************************************

好了,基本用法已经讲完了,现在来看看一些注意点和知识点吧。

首先来看看attrs.xml文件。

该文件是定义属性名和格式的地方,需要用<declare-styleable name="ToolBar"></declare-styleable>包围所有属性。其中name为该属性集的名字,主要用途是标识该属性集。那在什么地方会用到呢?主要是在第三步。看到没?在获取某属性标识时,用到"R.styleable.ToolBar_buttonNum",很显然,他在每个属性前面都加了"ToolBar_"。

在来看看各种属性都有些什么类型吧:string , integer , dimension , reference , color , enum.

前面几种的声明方式都是一致的,例如:<attr name="buttonNum" format="integer"/>。

只有enum是不同的,用法举例:

<attr name="testEnum">

<enum name="fill_parent" value="-1"/>

<enum name="wrap_content" value="-2"/>

</attr>

如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。

让我们再来看看布局xml中需要注意的事项。

首先得声明一下:xmlns:toolbar=http://schemas.android.com/apk/res/cn.zzm.toolbar

注意,“toolbar”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“toolbar”即可。

最后来看看java代码中的注意事项。

在自定义组件的构造函数中,用

TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ToolBar);

来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了a.recycle();回收资源哦!

属性详解:

1. reference:参考某一资源ID。

(1)属性定义:

<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>

(2)属性使用:

<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>

2. color:颜色值。

(1)属性定义:

<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>

(2)属性使用:

<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>

3. boolean:布尔值。

(1)属性定义:

<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>

(2)属性使用:

<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>

4. dimension:尺寸值。

(1)属性定义:

<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>

(2)属性使用:

<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>

5. float:浮点值。

(1)属性定义:

<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>

(2)属性使用:

<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>

6. integer:整型值。

(1)属性定义:

<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>

(2)属性使用:

<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/图片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>

7. string:字符串。

(1)属性定义:

<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>

(2)属性使用:

<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"
/>

8. fraction:百分数。

(1)属性定义:

<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>

(2)属性使用:

<rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
               android:interpolator = "@anim/动画ID"
android:fromDegrees = "0"
               android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
               android:duration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>

9. enum:枚举值。

(1)属性定义:

<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>

(2)属性使用:

<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>

10. flag:位或运算。

(1)属性定义:

<declare-styleable name="名称">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
<flag name = "stateVisible" value = "4" />
<flag name = "stateAlwaysVisible" value = "5" />
<flag name = "adjustUnspecified" value = "0x00" />
<flag name = "adjustResize" value = "0x10" />
<flag name = "adjustPan" value = "0x20" />
<flag name = "adjustNothing" value = "0x30" />
</attr>
</declare-styleable>

(2)属性使用:

<activity
android:name = ".StyleAndThemeActivity"
android:label = "@string/app_name"
android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

注意:

属性定义时可以指定多种类型值。

(1)属性定义:

<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>

(2)属性使用:

<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"
/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息