码迷,mamicode.com
首页 > 移动开发 > 详细

android 自定义控件步骤

时间:2014-10-28 23:31:51      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   使用   for   

1、写一个类继承View或你想扩展功能的控件(比如TextView)。

          public class CustomView extends View { };

2、在/res/value下创建一个attr.xml文件。
没有这个文件自定义控件照样能显示出来,但只能使用所继承的父类中包含的属性,有了这个文件可以增加自定义的命名空间,来设置自定义的属性(其中format可选值见文尾)(大家看到这里可能觉得很突兀,坚持看完,相信你会明白这里的!)
<?xml version="1.0" encoding="utf-8"?>   
<resources>   
    <declare-styleable name="CustomView">   
        <attr name="textColor" format="color" />   
        <attr name="textSize" format="dimension" />   
    </declare-styleable>   
</resources> 
2、CustomView中重写父类的构造方法(我一般把三个都写上)在构造方法中获取到自定义是属性的值。
public CustomView(Context context) {
super(context);
}
//xml文件解析的时候,会把标签解析成一个类,标签里的属性及属性值都传递到AttributeSet里了,所以我们要从这里把属性值获取出来,设置给画笔。
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomView);//获取定义在attr中属性的属性值,这个属性值是在,把控件设置到界面上时所使用的layout中设置的
int color = array.getColor(R.styleable.CustomView_textColor, Color.RED);//第二个参数为默认值
float size = array.getDimension(R.styleable.CustomView_textSize, 10);


paint = new Paint();
paint.setColor(color);
paint.setTextSize(size);


array.recycle();//必须有!!清空原array,防止以后出现原来设置的属性。
}


4、重写onDraw()方法。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("我是被画出来的", 10, 100, paint);//第二个和第三个参数为坐标的X轴Y轴
}


5、新建一个Activity(此处不再给出),在其使用的布局文件中添加自定义控件,并且可以引入自定义的命名空间,使用attr中定义的属性。
<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:dinglin="http://schemas.android.com/apk/res/cn.itheima.customview"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical" >
 
    <!-- 使用自定义控件要用包名+类名的形式,其中命名控件dl中可以使用在attr.xml中定义的属性 -->
      <cn.itheima.customview.CustomView
        android:layout_width="fill_parent"
         android:layout_height="fill_parent"
        dl:textColor="#ff00ff00"
       dl:textSize="25dip" >
    </cn.itheima.customview.CustomView>
</LinearLayout>


搞定!运行就可以看到自己定义的控件了,这个控件有些丑,文章开头已经说了,只是简单介绍自定义控件的开发流程!勿怪!
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Android自定义属性时format选项可以取用的值
1. reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name="名称"> 
<attr format="reference" name="background" />
</declare-styleable>


(2)属性使用:
<ImageView 
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID" />


2. color:颜色值。
(1)属性定义:
<declare-styleable name="名称"> 
<attr format="color" name="textColor" />
</declare-styleable>
(2)属性使用:
<TextView 
android:layout_width="42dip"
android:layout_height="42dip"
android:textColor="#00FF00" />


3. boolean:布尔值。
(1)属性定义:
<declare-styleable name="名称"> 
<attr format="boolean" name="focusable" />
</declare-styleable>
(2)属性使用:
<Button 
android:layout_width="42dip"
android:layout_height="42dip"
android:focusable="true" />


4. dimension:尺寸值。
(1)属性定义:
<declare-styleable name="名称"> 
<attr format="dimension" name="layout_width" />
</declare-styleable>


(2)属性使用:
<Button 
android:layout_width="42dip"
android:layout_height="42dip" />
5. float:浮点值。
(1)属性定义:
<declare-styleable name="AlphaAnimation">
<attr format="float" name="fromAlpha" />
<attr format="float" name="toAlpha" />
</declare-styleable>


(2)属性使用:
<alpha 
android:fromAlpha="1.0"
android:toAlpha="0.7" />
6. integer:整型值。
(1)属性定义:
<declare-styleable name="AnimatedRotateDrawable">
<attr format="integer" name="frameDuration" />
<attr format="integer" name="framesCount" />
</declare-styleable>
(2)属性使用:
[html] view plaincopyprint?
<animated-rotate 
android:frameDuration="100"
android:framesCount="12"/>
7. string:字符串。
(1)属性定义:
<declare-styleable name="MapView">
<attr format="string" name="apiKey" />
</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 format="fraction" name="pivotX" />
<attr format="fraction" name="pivotY" />
</declare-styleable>
(2)属性使用:
<rotate 
android:pivotX="200%"
android:pivotY="300%"/>
9. enum:枚举值。
(1)属性定义:
<declare-styleable name="名称"> 
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr> 
</declare-styleable>
(2)属性使用:
<LinearLayout 
android:orientation="vertical" >
</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:windowSoftInputMode="stateUnspecified | stateUnchanged | stateHidden" >
</activity> 
注意:属性定义时可以指定多种类型值:
(1)属性定义:
<declare-styleable name="名称"> 
<attr format="reference|color" name="background" />
</declare-styleable>
(2)属性使用:
<ImageView 
android:layout_width="42dip"
android:layout_height="42dip"
android:background="@drawable/图片ID|#00FF00" />

 

                                                                                   转自:http://blog.csdn.net/dinglin_87/article/details/7431545

 

android 自定义控件步骤

标签:android   style   blog   http   io   color   ar   使用   for   

原文地址:http://www.cnblogs.com/liuluchao/p/4058060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!