Android 自訂控制項

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

(屬性其實就一是一對Key-value的玩意,然後在代碼中對這些進行控制,Android的AttributeSet 讓我想起了dojo中的玩意,這個其實就是一個索引值對的集合,在建構函式中傳入的……)

所謂自訂控制項(或稱組件)也就是編寫自己的控制項類型,而非Android中提供的標準的控制項,如TextView,CheckBox等等.不過自訂的控制項一般也都是從標準控制項繼承來的,或者是多種控制群組合,或者是對標準控制項的屬性進行改變而得到的自己滿意的控制項.

    自訂控制項可能會有很多種方法,這裡只介紹我要介紹的方法.

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

    1.我們的自訂控制項和其他的控制項一樣,應該寫成一個類,而這個類的屬性是是有自己來決定的.

    2.我們要在res/values目錄下建立一個attrs.xml的檔案,並在此檔案中增加對控制項的屬性的定義.

    3.使用AttributeSet來完成控制項類的建構函式,並在建構函式中將自訂控制項類中變數與attrs.xml中的屬性串連起來.

    4.在自訂控制項類中使用這些已經串連的屬性變數.

    5.將自訂的控制項類定義到布局用的xml檔案中去.

    6.在介面中產生此自訂控制項類對象,並加以使用.

 

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

    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);  
            //設定填充  
            mPaint.setStyle(Style.FILL);  
          
            //畫一個矩形,前倆個是矩形左上方座標,後面倆個是右下角座標  
            canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
          
            mPaint.setColor(Color.BLUE);  
            //繪製文字  
            canvas.drawText(mString, 10, 110, mPaint);  
        }  
    } 

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

 

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

    2. 在res/values目錄下建立一個attrs.xml的檔案,並在此檔案中增加對控制項的屬性的定義--代碼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>標籤下使用<declare-styleable name="MyView">標籤來告訴架構它包含的屬性就是自訂控制項MyView中的屬性.黃色的兩其實就對應了代碼1中黃色的變數.

 

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

    3.使用AttributeSet來完成控制項類的建構函式,並在建構函式中將自訂控制項類中變數與attrs.xml中的屬性串連起來.

    我們再看一下代碼1中的藍色代碼,其中使用AttributeSet來重載建構函式.在此函數中將類中的屬性變數與代碼二中定義的屬性聯絡起來.

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

    4.在自訂控制項類中使用這些已經串連的屬性變數.

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

 

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

    5.將自訂的控制項類定義到布局用的xml檔案中去.-----代碼3:

    我們再看看布局的xml檔案代碼:

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/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" test:textSize="20px" test:textColor="#fff" />  
    </LinearLayout>
    其中紅色部分在布局中引用了我們MyView控制項.

 

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

    6.在介面中產生此自訂控制項類對象,並加以使用.--------代碼4.

 

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

Android 自訂控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.