posts - 93,  comments - 2,  trackbacks - 0
          所謂自定義控件(或稱組件)也就是編寫自己的控件類型,而非Android中提供的標(biāo)準(zhǔn)的控件,如TextView,CheckBox等等.不過自定義的控件一般也都是從標(biāo)準(zhǔn)控件繼承來的,或者是多種控件組合,或者是對(duì)標(biāo)準(zhǔn)控件的屬性進(jìn)行改變而得到的自己滿意的控件.

              自定義控件可能會(huì)有很多種方法,這里只介紹我要介紹的方法.

           

              在這種方法中,大概的步驟是這樣的

              1.我們的自定義控件和其他的控件一樣,應(yīng)該寫成一個(gè)類,而這個(gè)類的屬性是是有自己來決定的.

              2.我們要在res/values目錄下建立一個(gè)attrs.xml的文件,并在此文件中增加對(duì)控件的屬性的定義.

              3.使用AttributeSet來完成控件類的構(gòu)造函數(shù),并在構(gòu)造函數(shù)中將自定義控件類中變量與attrs.xml中的屬性連接起來.

              4.在自定義控件類中使用這些已經(jīng)連接的屬性變量.

              5.將自定義的控件類定義到布局用的xml文件中去.

              6.在界面中生成此自定義控件類對(duì)象,并加以使用.

           

              好了,按照上述的方法,我們來看看http://blog.csdn.net/Android_Tutor/archive/2010/04/20/5508615.aspx

              博客中的實(shí)例代碼,按步驟加以解釋:

              //---------------------------------------------------------------------------------

              1. 定義自己的控件類:--------------------------------------------代碼1.

              package com.android.tutor;  
              import android.content.Context;  
              import android.content.res.TypedArray;  
              import android.graphics.Canvas;  
              import android.graphics.Color;  
              import android.graphics.Paint;  
              import android.graphics.Rect;  
              import android.graphics.Paint.Style;  
              import android.util.AttributeSet;  
              import android.view.View;  

           
              public class MyView extends View
               
                  private Paint mPaint;  
                  private Context mContext;  
                  private static final String mString = "Welcome to Mr Wei's blog";  
                
                  public MyView(Context context)
                   
                      super(context);  
                      mPaint = new Paint();  
                   

           
                  public MyView(Context context,AttributeSet attrs)  
                   
                      super(context,attrs);  
                      mPaint = new Paint();  
                    
                      TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);             
                      int textColor = a.getColor(R.styleable.MyView_textColor,0XFFFFFFFF);  
                      float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
                    
                      mPaint.setTextSize(textSize);  
                      mPaint.setColor(textColor);  
                    
                      a.recycle();  
                  }

             
                  @Override
                  protected void onDraw(Canvas canvas)

                   
                      // TODO Auto-generated method stub  
                      super.onDraw(canvas);  
                      //設(shè)置填充  
                      mPaint.setStyle(Style.FILL);  
                    
                      //畫一個(gè)矩形,前倆個(gè)是矩形左上角坐標(biāo),后面?zhèn)z個(gè)是右下角坐標(biāo)  
                      canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
                    
                      mPaint.setColor(Color.BLUE);  
                      //繪制文字  
                      canvas.drawText(mString, 10, 110, mPaint);  
                  }
            
              

              代碼1定義了一個(gè)自定義控件,名字為MyView,是從View類繼承而來,也就是說它本身就是一種View,只是在View基礎(chǔ)上加工而成了我們自己的自定義控件MyView.在此類種黃色的兩行變量是我們新的屬性變量.

           

              //---------------------------------------------------------------------------------

              2. 在res/values目錄下建立一個(gè)attrs.xml的文件,并在此文件中增加對(duì)控件的屬性的定義--代碼2:

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

              在<resources>標(biāo)簽下使用<declare-styleable name="MyView">標(biāo)簽來告訴框架它包含的屬性就是自定義控件MyView中的屬性.黃色的兩其實(shí)就對(duì)應(yīng)了代碼1中黃色的變量.

           

              //---------------------------------------------------------------------------------

              3.使用AttributeSet來完成控件類的構(gòu)造函數(shù),并在構(gòu)造函數(shù)中將自定義控件類中變量與attrs.xml中的屬性連接起來.

              我們?cè)倏匆幌麓a1中的藍(lán)色代碼,其中使用AttributeSet來重載構(gòu)造函數(shù).在此函數(shù)中將類中的屬性變量與代碼二中定義的屬性聯(lián)系起來.

              //---------------------------------------------------------------------------------

              4.在自定義控件類中使用這些已經(jīng)連接的屬性變量.

              我們看一下代碼1中的黃色部分,就是對(duì)我們新定義的屬性的使用.

           

              //---------------------------------------------------------------------------------

              5.將自定義的控件類定義到布局用的xml文件中去.-----代碼3:

              我們?cè)倏纯床季值膞ml文件代碼:

              <?xml version="1.0" encoding="utf-8"?>  
              <LinearLayout xmlns:android="
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"  
                  <TextView android:layout_width="fill_parent"   
                      android:layout_height="wrap_content"   
                      android:text="@string/hello" />  

                  <com.android.tutor.MyView  android:layout_width="fill_parent"   
                      android:layout_height="fill_parent" app:textSize="20px" app:textColor="#fff" />  
              </LinearLayout>
              其中紅色部分在布局中引用了我們MyView控件.

           

              //---------------------------------------------------------------------------------

              6.在界面中生成此自定義控件類對(duì)象,并加以使用.--------代碼4.

           

              //---------------------------------------------------------------------------------

          轉(zhuǎn)載:
          http://www.cnblogs.com/zwl12549/archive/2011/04/13/2015366.html

          posted on 2015-04-14 10:53 Terry Zou 閱讀(331) 評(píng)論(0)  編輯  收藏 所屬分類: Android
          <2015年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          Java

          搜索

          •  

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 云梦县| 汉中市| 余江县| 安宁市| 衡山县| 陆良县| 焉耆| 霍山县| 修文县| 临沭县| 西贡区| 类乌齐县| 盐亭县| 临西县| 龙泉市| 长垣县| 天门市| 六枝特区| 拜城县| 安顺市| 黄冈市| 池州市| 牡丹江市| 柳林县| 湾仔区| 玉溪市| 黄冈市| 嘉荫县| 永定县| 高台县| 蒙自县| 淳安县| 苍溪县| 如皋市| 桦南县| 洛浦县| 辰溪县| 葫芦岛市| 靖安县| 荔波县| 红河县|