Android 自訂控制項

來源:互聯網
上載者:User

標籤:

在安卓體系中,所有控制項及布局都是View的子類或間接子類,如Button繼承TextView,TextView繼承了View。

 

如果我們想定義一個控制項,必須繼承View或View的子類。

 

如果想對現有控制項進行拓展,則繼承該控制項,修改內部實現即可。

如果想建立新控制項,則繼承View即可。

 

如,安卓控制項體繫結構

 

首先,我們以拓展TextView為例,看一下如何定義我們自己的TextView

 

自訂一個類,繼承TextView

 

1 public class MyTextView extends TextView {2     public MyTextView(Context context, AttributeSet attrs) {//系統根據XML建立控制項時候調用3         super(context, attrs);4     }5     public MyTextView(Context context) {//使用代碼自己建立對象的時候調用6         super(context);7     }8 }

 

一般我們都是把空間配置到XML布局檔案裡,所以一般我們都是使用上邊的建構函式。

 

然後重寫onDraw方法,繪製介面。

 

 1 @Override 2     protected void onDraw(Canvas canvas) { 3         Paint paint_blue = new Paint();//畫筆 4         paint_blue.setColor(Color.BLUE);//藍色 5         paint_blue.setStyle(Style.STROKE);//空心,FILL(實心) 6         paint_blue.setStrokeWidth(10);//線寬度 7         canvas.drawCircle(110,150,60,paint_blue);//繪製 8      9         Paint paint_text = new Paint(); //繪製字串10         paint_text.setColor(Color.BLUE);11         paint_text.setTextSize(40);12         canvas.drawText("四海興唐專用標籤", 215, 190, paint_text);13         super.onDraw(canvas);14     }

 

下面圖就是我們繪製的TextView,除了設定的文本之外,還多了一個圓圈和藍色的文字

 

如果我們想向安卓系統內建控制項那樣可以設定屬性,需要在values/attrs.xml裡設定如下

 

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name = "customView">        <attr name = "radius" format="integer" />        <attr name = "textColor" format="color" />    </declare-styleable></resources>

定義了2個屬性,

  1. radius,約束為整型
  2. textColor,約束為顏色

這些屬性我們可以在控制項的構造方法裡使用,如下

public MyTextView(Context context, AttributeSet attrs) {        super(context, attrs);        //擷取屬性集合        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.customView);    //擷取customView中的radius屬性,如果沒有,則預設10        radius = a.getInt(R.styleable.customView_radius,10);    //擷取customView中的textColor屬性,如果沒有,則預設藍色        color = a.getColor(R.styleable.customView_textColor, Color.BLUE);    //回收TypedArray,在調用這個函數後,就不能再使用這個TypedArray。    a.recycle();}

 

擷取之後,就可以在onDraw方法裡使用了。

如下

 

@Override    protected void onDraw(Canvas canvas) {        Paint paint_blue = new Paint();//畫筆        paint_blue.setColor(color);//使用自訂色彩        paint_blue.setStyle(Style.STROKE);//空心        paint_blue.setStrokeWidth(10);//線寬度        canvas.drawCircle(110,150,radius,paint_blue);//繪製圓形,使用自訂半徑        Paint paint_text = new Paint(); //繪製字串        paint_text.setColor(Color.BLUE);        paint_text.setTextSize(40);        canvas.drawText("四海興唐專用標籤", 215, 190, paint_text);        super.onDraw(canvas);    }

這些屬性我們定義控制項的時候就可以設定了,

在這之前,需要添加如下代碼到根布局裡,其中toolbar是定義屬性的首碼

xmlns:toolbar=http://schemas.android.com/apk/res-auto

 

然後就可以設定我們自己的控制項和屬性了

 

<com.ccshxt.shxt.MyTextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@+id/message"        android:text="普通文本"        toolbar:radius="40"        toolbar:textColor="#FF0000"/>

 

這裡的控制項名,就是我們定義的類,注意別忘了包名。

 

效果如

 

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.