編輯:關於 Android 開發
近來在開發一個音樂播放應用,寫到播放介面的 UI 時,就想自己實作一個播放介面。那麼究竟該如何實作自訂 View 呢?透過查閱他人部落格與 Android 官方開發文件,初步了解了一些基礎內容,在此記錄,以供需要的朋友參考。
自訂視圖的官方參考文件:https://developer.android.com/training/custom-views/create-view.html
當我們使用 XML 佈局檔編寫 UI 時,透過增加 XML tag,我們可以加入任何想要的 View 元件。例如下列的佈局檔(只是個例子,刪除了其他不重要的佈局元素):
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <TextView 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" /> 8 </RelativeLayout>
透過 <RelativeLayout> 引入 RelativeLayout,透過 <TextView> 引入 TextView,然後加入各種屬性(如 android:layout_width、android:layout_height),編譯程式在編譯時會解析 XML 檔,把對應的 tag 生成相應的 View 物件。
那麼自訂的 View 要如何透過上述方式使用呢?這就要談到 xmlns(XML namespace)了。了解 XML 的朋友都知道,XML 為了傳輸資料而設計為可擴展的標記語言,它不同於 HTML 有預定義的各種標籤,它的標籤是由使用者自訂的。在哪裡定義呢?就在 xmlns!
不知道你有沒有注意到,我們的佈局檔中總會有這樣一行程式碼:
xmlns:android="https://schemas.android.com/apk/res/android"
這就是引入命名空間。簡單來說,透過在頂級 tag 中引入命名空間,整個 XML 就都能使用該命名空間中定義的內容,例如 android:id 等。那麼要怎麼為自己寫的 View 添加命名空間呢?
首先在 res → values 目錄下新增 attrs 資源檔,在其中定義需要表述的屬性:

1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="Circle"> 4 <attr name="radius" format="dimension"/> 5 <attr name="background_color" format="color"/> 6 </declare-styleable> 7 </resources>
其中 declare-styleable 節點的 name 屬性值一般是你寫的 View 的名稱,這裡自訂的 View 名為 Circle。接著定義可在 XML 中設定的元件屬性,這裡指定了 radius 和 background 兩個屬性。其中 format 屬性用來指定可接受值的類型,多個類型可用「|」分隔。更多關於 attrs 的寫法,可參考 Android SDK 目錄下 platforms\android-xxx\data\res\values\attrs.xml 檔案。
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 xmlns:custom="https://schemas.android.com/apk/res-auto" 4 xmlns:tools="https://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:paddingBottom="@dimen/activity_vertical_margin" 8 android:paddingLeft="@dimen/activity_horizontal_margin" 9 android:paddingRight="@dimen/activity_horizontal_margin" 10 android:paddingTop="@dimen/activity_horizontal_margin" 11 tools:context="com.github.fallblank.myapplication.MainActivity"> 12 <!--引入自定義View,並設置屬性--> 13 <com.github.fallblank.myapplication.Circle 14 android:id="@+id/circle" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 custom:background_color="#ff0000" 18 custom:radius="100dp" /> 19 <!--下面兩個按鈕是為了改變View使其重繪--> 20 <Button 21 android:id="@+id/btn_larger" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_alignParentBottom="true" 25 android:text="Larger" /> 26 27 <Button 28 android:id="@+id/btn_smaller" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:layout_toRightOf="@id/btn_larger" 32 android:layout_alignParentBottom="true" 33 android:text="Smaller" /> 34 </RelativeLayout>
注意這行程式碼 xmlns:custom="https://schemas.android.com/apk/res-auto",它就是引入自訂的命名空間,有了這行之後,後面就可以使用之前定義的 custom:radius 和 custom:background_color 了。需要注意的是,此引入方式適用於 gradle 編譯;若對應 ant 編譯,xmlns 定義如下:
xmlns:custom="https://schemas.android.com/apk/res/自定義view的包名
1 package com.github.fallblank.myapplication; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.util.AttributeSet; 8 import android.view.View; 9 import android.widget.Toast; 10 11 /** 12 * Created by fallb on 2015/11/24. 13 */ 14 public class Circle extends View { 15 16 private int mColor; 17 private Context mContext; 18 private float mRadius; 19 20 public Circle(Context context, AttributeSet attrs) { 21 super(context, attrs); 22 mContext = context; 23 TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.Circle,0,0); 24 try { 25 // 26 mColor = a.getColor(R.styleable.Circle_line_color,0x000000); 27 mRadius = a.getDimension(R.styleable.Circle_side,16); 28 }finally { 29 a.recycle(); 30 } 31 } 32 33 public void addSide() { 34 mRadius+=5.0; 35 invalidate(); 36 requestLayout(); 37 } 38 39 public void subSide(){ 40 if (mRadius>=5.0){ 41 mRadius-=5.0; 42 invalidate(); 43 requestLayout(); 44 }else { 45 Toast.makeText(mContext,"smaller enough!",Toast.LENGTH_LONG).show(); 46 } 47 } 48 49 @Override 50 protected void onDraw(Canvas canvas) { 51 super.onDraw(canvas); 52 Paint paint = new Paint(); 53 paint.setColor(mColor); 54 canvas.drawCircle(0,0,mRadius,paint); 55 } 56 }
取得屬性後,接著重寫 onDraw() 來實作自己的繪製方法。關於 Paint 與 Canvas 的具體用法,將在後續文章中介紹。
最後附上效果圖與範例程式碼:


範例程式碼:https://github.com/fallblank/CustomView
僅此拋磚引玉,歡迎交流指正!
Android 面試準備:SharedPreferences 與文件存儲
Android 面試準備:SharedPreferences 與文件存儲 Android 常見的輕量資料保存方式包括文件存儲與 SharedPreferences。兩者都
用全域象徵斷點自動掛載 Reveal,換專案不必重新設定
網路上多數 Reveal 的載入教學,都是把 Reveal.framework 拖進專案裡,缺點很明顯:每換一個專案就得重做一次。其實只要在 Xcode 裡設定一個「全域
FloatingActionButton 進階用法:一個主按鈕展開多個子按鈕(開源元件實作)
今天要介紹的是 FloatingActionButton 的進階玩法——透過 FloatingActionsMenu 容器,讓一個懸浮按鈕點擊後展開一整組子按鈕,是 Ma
用 shape 定義 Android 控制項外觀:屬性全解析與 Selector 範例
用 shape 定義 Android 控制項外觀:屬性全解析與 Selector 範例 Android 中經常使用 shape 來定義控制項的顯示屬性。相比拉一張切圖進來