Android(安卓)自定义组件之如何实现自定义组件
参考链接:http://blog.csdn.net/jjwwmlp456/article/details/41076699
简介
Android提供了用于构建UI的强大的组件模型。两个基类:View和ViewGroup。
可用Widget的部分名单包括Button, TextView, EditText, ListView, CheckBox,RadioButton, Gallery, Spinner,以及一些有特别作用的组件: AutoCompleteTextView, ImageSwitcher和 TextSwitcher。
可用的布局有:LinearLayout,FrameLayout,RelativeLayout,AbsoluteLayout,GridLayout (later on api level 14 or v7-support)
基本做法
1. 继承自View或View的子类
2. 重写父类的一些方法,如:onDraw(),onMeasure(),onLayout()等
3. 使用自定义的组件类。
1. 最普通的作法是,继承自View,实现你的自定义组件
2. 提供一个构造函数,采用有属性参数的,也可以使用自定义属性
3. 你可能想在组件中创建自己的事件监听器,属性访问器和修改器,或其他行为
4. 几乎肯定要重写onDraw(),onMeasure()。默认onDraw()什么也没作,onMeasure()则设置一个100x100的尺寸。
5. 根据需要重写其他方法 ...
onDraw()和onMeasure()
onDraw(),提供一个Canvas,可以绘制2D图形。
若要绘制3D图形,请继承GLSurfaceView,参见,api-demo下的GLSurfaceViewActivity
onMeasure()测量组件
1. 宽度和高度在需要测量时调用该方法
2.应该进行测量计算组件将需要呈现的宽度和高度。它应该尽量保持传入的规格范围内,尽管它可以选择超过它们(在这种情况下,父视图可以选择做什么,包括裁剪,滚动,抛出一个异常,或者要求onMeasure()再次尝试,或使用不同的测量规格)
3. 宽高计算完毕后,必须调用用setMeasuredDimession(int width, int height),进行设置。否则将抛出一个异常
下面是一些View中可被调用的方法总结(未全部包含,可自行查看类似onXxx的方法):
Category | Methods | Description |
---|---|---|
Creation | Constructors | There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file. |
| Called after a view and all of its children has been inflated from XML. | |
Layout |
| Called to determine the size requirements for this view and all of its children. |
| Called when this view should assign a size and position to all of its children. | |
| Called when the size of this view has changed. | |
Drawing |
| Called when the view should render its content. |
Event processing |
| Called when a new key event occurs. |
| Called when a key up event occurs. | |
| Called when a trackball motion event occurs. | |
| Called when a touch screen motion event occurs. | |
Focus |
| Called when the view gains or loses focus. |
| Called when the window containing the view gains or loses focus. | |
Attaching |
| Called when the view is attached to a window. |
| Called when the view is detached from its window. | |
| Called when the visibility of the window containing the view has changed. |
自定义View示例
adi-demo下的示例:LabelView
[java]view plaincopyprint?
- /*
- *Copyright(C)2007TheAndroidOpenSourceProject
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");
- *youmaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- */
- packageandroid.widget;
- importandroid.content.Context;
- importandroid.graphics.Canvas;
- importandroid.graphics.Paint;
- importandroid.view.View;
- /**
- *ExampleofhowtowriteacustomsubclassofView.LabelView
- *isusedtodrawsimpletextviews.Notethatitdoesnothandle
- *styledtextorright-to-leftwritingsystems.
- *
- */
- publicclassLabelViewextendsView{
- /**
- *Constructor.Thisversionisonlyneededifyouwillbeinstantiating
- *theobjectmanually(notfromalayoutXMLfile).
- *@paramcontexttheapplicationenvironment
- */
- publicLabelView(Contextcontext){
- super(context);
- initLabelView();
- }
- /**
- *Constructobject,initializingwithanyattributesweunderstandfroma
- *layoutfile.Theseattributesaredefinedin
- *SDK/assets/res/any/classes.xml.
- *
- *@seeandroid.view.View#View(android.content.Context,android.util.AttributeSet)
- publicLabelView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- initLabelView();
- Resources.StyledAttributesa=context.obtainStyledAttributes(attrs,
- R.styleable.LabelView);
- CharSequences=a.getString(R.styleable.LabelView_text);
- if(s!=null){
- setText(s.toString());
- }
- ColorStateListtextColor=a.getColorList(R.styleable.
- LabelView_textColor);
- if(textColor!=null){
- setTextColor(textColor.getDefaultColor(0));
- }
- inttextSize=a.getInt(R.styleable.LabelView_textSize,0);
- if(textSize>0){
- setTextSize(textSize);
- }
- a.recycle();
- }
- */
- privatevoidinitLabelView(){
- mTextPaint=newPaint();
- mTextPaint.setAntiAlias(true);
- mTextPaint.setTextSize(16);
- mTextPaint.setColor(0xFF000000);
- mPaddingLeft=3;
- mPaddingTop=3;
- mPaddingRight=3;
- mPaddingBottom=3;
- }
- /**
- *Setsthetexttodisplayinthislabel
- *@paramtextThetexttodisplay.Thiswillbedrawnasoneline.
- */
- publicvoidsetText(Stringtext){
- mText=text;
- requestLayout();
- invalidate();
- }
- /**
- *Setsthetextsizeforthislabel
- *@paramsizeFontsize
- */
- publicvoidsetTextSize(intsize){
- mTextPaint.setTextSize(size);
- requestLayout();
- invalidate();
- }
- /**
- *Setsthetextcolorforthislabel
- *@paramcolorARGBvalueforthetext
- */
- publicvoidsetTextColor(intcolor){
- mTextPaint.setColor(color);
- invalidate();
- }
- /**
- *@seeandroid.view.View#measure(int,int)
- */
- @Override
- protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
- setMeasuredDimension(measureWidth(widthMeasureSpec),
- measureHeight(heightMeasureSpec));
- }
- /**
- *Determinesthewidthofthisview
- *@parammeasureSpecAmeasureSpecpackedintoanint
- *@returnThewidthoftheview,honoringconstraintsfrommeasureSpec
- */
- privateintmeasureWidth(intmeasureSpec){
- intresult;
- intspecMode=MeasureSpec.getMode(measureSpec);
- intspecSize=MeasureSpec.getSize(measureSpec);
- if(specMode==MeasureSpec.EXACTLY){
- //Weweretoldhowbigtobe
- result=specSize;
- }else{
- //Measurethetext
- result=(int)mTextPaint.measureText(mText)+mPaddingLeft
- +mPaddingRight;
- if(specMode==MeasureSpec.AT_MOST){
- //RespectAT_MOSTvalueifthatwaswhatiscalledforbymeasureSpec
- result=Math.min(result,specSize);
- }
- }
- returnresult;
- }
- /**
- *Determinestheheightofthisview
- *@parammeasureSpecAmeasureSpecpackedintoanint
- *@returnTheheightoftheview,honoringconstraintsfrommeasureSpec
- */
- privateintmeasureHeight(intmeasureSpec){
- intresult;
- intspecMode=MeasureSpec.getMode(measureSpec);
- intspecSize=MeasureSpec.getSize(measureSpec);
- mAscent=(int)mTextPaint.ascent();
- if(specMode==MeasureSpec.EXACTLY){
- //Weweretoldhowbigtobe
- result=specSize;
- }else{
- //Measurethetext(beware:ascentisanegativenumber)
- result=(int)(-mAscent+mTextPaint.descent())+mPaddingTop
- +mPaddingBottom;
- if(specMode==MeasureSpec.AT_MOST){
- //RespectAT_MOSTvalueifthatwaswhatiscalledforbymeasureSpec
- result=Math.min(result,specSize);
- }
- }
- returnresult;
- }
- /**
- *Renderthetext
- *
- *@seeandroid.view.View#onDraw(android.graphics.Canvas)
- */
- @Override
- protectedvoidonDraw(Canvascanvas){
- super.onDraw(canvas);
- canvas.drawText(mText,mPaddingLeft,mPaddingTop-mAscent,mTextPaint);
- }
- privatePaintmTextPaint;
- privateStringmText;
- privateintmAscent;
- }
应用该自定义组件的layout xml:
[html]view plaincopyprint?
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <com.example.android.apis.view.LabelView
- android:background="@drawable/red"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:text="Red"/>
- <com.example.android.apis.view.LabelView
- android:background="@drawable/blue"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:text="Blue"app:textSize="20dp"/>
- <com.example.android.apis.view.LabelView
- android:background="@drawable/green"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:text="Green"app:textColor="#ffffffff"/>
- </LinearLayout>
该示例演示了:
1. 继承自View的完全自定义组件
2. 带参数的构造函数(一些属性参数在xml中设置)。还使用了自定义属性R.styleable.LabelView
3. 一些标准的public 方法,如setText()、setTextSize()、setTextColor()
4. onMeasure()测量组件尺寸,内部由measureWidth(intmeasureSpec) 和measureHeight(intmeasureSpec)来测量。
5. onDraw()将Label绘制到画面Canvas上
复合组件
由一些现有组件,复合成一个新的组件。 要创建一个复合组件: 1. 通常需要创建一个类,继承自一个Layout,或者ViewGroup。 2. 在构造函数中,需要先调用父类相应的构造函数。然后设置一些需要的组件用于复合。可以使用自定义属性 3. 可以创建监听器,监听处理一些可能的动作 4. 可能有一些 属性的 get / set 方法 5. 如果继承自某一Layout类时,不需要重写onDraw()和onMeasure(),因为Layout类中有默认的行为。如有必要,当然也可以重写 6. 可能重写其他一些onXxx(),以达到你想要的效果复合组件示例
api-demo下的List4和List6里的内部类SpeachView,以下为List6中的源码 [java]view plaincopyprint?- privateclassSpeechViewextendsLinearLayout{
- publicSpeechView(Contextcontext,Stringtitle,Stringdialogue,booleanexpanded){
- super(context);
- this.setOrientation(VERTICAL);
- //Herewebuildthechildviewsincode.Theycouldalsohave
- //beenspecifiedinanXMLfile.
- mTitle=newTextView(context);
- mTitle.setText(title);
- addView(mTitle,newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
- mDialogue=newTextView(context);
- mDialogue.setText(dialogue);
- addView(mDialogue,newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
- mDialogue.setVisibility(expanded?VISIBLE:GONE);
- }
- /**
- *ConveniencemethodtosetthetitleofaSpeechView
- */
- publicvoidsetTitle(Stringtitle){
- mTitle.setText(title);
- }
- /**
- *ConveniencemethodtosetthedialogueofaSpeechView
- */
- publicvoidsetDialogue(Stringwords){
- mDialogue.setText(words);
- }
- /**
- *Conveniencemethodtoexpandorhidethedialogue
- */
- publicvoidsetExpanded(booleanexpanded){//该方法在List4中没有
- mDialogue.setVisibility(expanded?VISIBLE:GONE);
- }
- privateTextViewmTitle;
- privateTextViewmDialogue;
- }
修改现有View类型
继承自一个现有的View,以增强其功能,满足需要。sdk中有个记事本NotePad的示例工程。其中有一个类就是扩展了EditText。 在NoteEditor类中: [java]view plaincopyprint?
- publicstaticclassLinedEditTextextendsEditText{
- privateRectmRect;
- privatePaintmPaint;
- //ThisconstructorisusedbyLayoutInflater
- publicLinedEditText(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- //CreatesaRectandaPaintobject,andsetsthestyleandcolorofthePaintobject.
- mRect=newRect();
- mPaint=newPaint();
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setColor(0x800000FF);
- }
- /**
- *ThisiscalledtodrawtheLinedEditTextobject
- *@paramcanvasThecanvasonwhichthebackgroundisdrawn.
- */
- @Override
- protectedvoidonDraw(Canvascanvas){
- //GetsthenumberoflinesoftextintheView.
- intcount=getLineCount();//edittext中有几行,edittext继承textview
- //GetstheglobalRectandPaintobjects
- Rectr=mRect;
- Paintpaint=mPaint;
- /*
- *DrawsonelineintherectangleforeverylineoftextintheEditText
- */
- for(inti=0;i<count;i++){
- //Getsthebaselinecoordinatesforthecurrentlineoftext
- intbaseline=getLineBounds(i,r);//将一行的范围坐标赋给矩形r;返回一行y方向上的基准线坐标
- /*
- *Drawsalineinthebackgroundfromtheleftoftherectangletotheright,
- *ataverticalpositiononedipbelowthebaseline,usingthe"paint"object
- *fordetails.
- */
- canvas.drawLine(r.left,baseline+1,r.right,baseline+1,paint);//绘制一条线,宽度为原行的宽度,高度为从基线开始+1个像素
- }
- //Finishesupbycallingtheparentmethod
- super.onDraw(canvas);
- }
- }
定义
一个public的静态内部类,以便它可以被访问:NoteEditor.MyEditText 它是静态内部类,意味着,它不依靠外部类的成员,不会产生一些“组合的方法”。继承自EditText
类的初始化
构造函数中,先调用父类的构造方法,并且它是带属性参数的构造函数。在使用时,从一个xml布局文件inflate重写的方法
只有onDraw()被重写。在onDraw()中绘制了一条蓝色的线,该线从每行文本的的基线开始向下1像素,宽度为行宽。 方法结束前,调用super.onDraw()使用自定义组件
[html]view plaincopyprint?- <viewxmlns:android="http://schemas.android.com/apk/res/android"
- class="com.example.android.notepad.NoteEditor$LinedEditText"
- android:id="@+id/note"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@android:color/transparent"
- android:padding="5dp"
- android:scrollbars="vertical"
- android:fadingEdge="vertical"
- android:gravity="top"
- android:textSize="22sp"
- android:capitalize="sentences"
- />
使用完全限定类名,引入自定义组件。使用$引用内部类。
更多相关文章
- Android设置全屏隐藏状态栏的方法
- 使用adb出现假emulator的解决方法
- h5
- Android(安卓)LayoutInflater深度解析
- android 中使用Java反射Reflect,输出类中变量值,方便调试。
- android获取状态栏的高度。
- Android(安卓)Intent学习
- android中BaseActivity的公共方法
- Android:Smali语法中文介绍