編輯:關於 Android 程式設計
當系統自帶屬性無法滿足需求時,就得替控制項加上自定義屬性。這裡示範一個很典型的例子:讓文字方塊能單獨顯示左、上、右、下任一邊的邊框線。不用自定義屬性當然也做得到,但把「要不要邊框、哪一邊要有」開放出來,控制項就能被到處複用。
流程同樣可以歸成三步:添加屬性檔、在佈局中使用、依屬性值執行相應繪製。
先看工程結構:

在 values 資料夾下新建 attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定義屬性開始 --> <declare-styleable name="BorderTextView"> <attr name="layout_borders" format="boolean"></attr> <attr name="layout_borderLeft" format="boolean"></attr> <attr name="layout_borderTop" format="boolean"></attr> <attr name="layout_borderRight" format="boolean"></attr> <attr name="layout_borderBottom" format="boolean"></attr> </declare-styleable> <!-- 自定義屬性結束 --> </resources>
幾個要點:根節點是 <resources>;自定義屬性以 <declare-styleable> 起頭,它代表一個屬性集合,可以包含多個屬性,這裡 name 是集合名;集合內每個 <attr> 的 name 是屬性名,format 則約束取值類型。這裡全部是 boolean,所以只能填 true 或 false。format 還支援 string、integer、dimension、reference 等多種類型。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:greendroid="https://schemas.android.com/apk/res/com.llew.e" android:orientation="vertical" android:layout_width="fill_parent" android:background="#ffffff" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:text="@string/hello" android:textColor="#000000" /> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左側帶有邊框" android:layout_margin="10dip" greendroid:layout_borderLeft="true" android:textSize="20sp" android:textColor="#aabbcc"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="頂部帶有邊框" android:layout_margin="10dip" greendroid:layout_borderTop="true" android:textSize="20sp" android:textColor="#bbccaa"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="右側帶有邊框" android:layout_margin="10dip" greendroid:layout_borderRight="true" android:textSize="20sp" android:textColor="#ccaabb"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="底部帶有邊框" android:layout_margin="10dip" greendroid:layout_borderBottom="true" android:textSize="20sp" android:textColor="#abcabc"> </com.llew.e.view.wedgit.BorderTextView> <com.llew.e.view.wedgit.BorderTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="四周帶有邊框" android:layout_margin="10dip" greendroid:layout_borders="true" android:textSize="20sp" android:textColor="#cbacba"> </com.llew.e.view.wedgit.BorderTextView> </LinearLayout>
使用自定義控制項就是「完整包名 + 類名」。要在佈局裡使用自定義屬性,根節點必須加上 xmlns:greendroid="https://schemas.android.com/apk/res/com.llew.e":冒號後面的 greendroid 只是前綴名稱,取什麼都行(別跟 android 撞即可),而後半段必須與 manifest 中的 package 一致。之後就能寫 greendroid:layout_borderLeft="true" 這樣的宣告。
自定義控制項最省事的做法就是繼承。這裡直接繼承 TextView,在它的基礎上擴充:
public class BorderTextView extends TextView { /** 四周是否帶有邊框 */ boolean borders = false; /** 左邊是否帶有邊框 */ boolean borderLeft = false; /** 頂部是否帶有邊框 */ boolean borderTop = false; /** 右側是否帶有邊框 */ boolean borderRight = false; /** 底部是否帶有邊框 */ boolean borderBottom = false; /** 邊框顏色 */ String textColor = "#ff000000"; public BorderTextView(Context context) { this(context, null); } public BorderTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public BorderTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // 獲取自定義屬性集 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView); borders = typedArray.getBoolean(R.styleable.BorderTextView_layout_borders, false); borderLeft = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderLeft, false); borderTop = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderTop, false); borderRight = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderRight, false); borderBottom = typedArray.getBoolean(R.styleable.BorderTextView_layout_borderBottom, false); textColor = attrs.getAttributeValue("https://schemas.android.com/apk/res/android", "textColor"); typedArray.recycle(); } @Override public void draw(Canvas canvas) { super.draw(canvas); Paint paint = new Paint(); int color = paint.getColor(); paint.setColor(Color.parseColor(textColor)); if (borders) { canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); } else { if (borderLeft) { canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint); } if (borderTop) { canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint); } if (borderRight) { canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1, this.getHeight() - 1, paint); } if (borderBottom) { canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1, this.getHeight() - 1, paint); } } paint.setColor(color); } }
原理很單純:每個 View 顯示到大致經歷 onMeasure、onLayout、onDraw 三個階段,邊框屬於純裝飾,所以在 draw 裡補畫即可,不必介入測量與配置。
這裡有幾處可以改進的地方,值得提出來:一是 Paint 每次都 new 一個,draw 是繪製高頻路徑,應該提成成員屬性複用;二是座標用 getHeight() - 1 而非 getHeight(),是為了避開右側與底部畫在同一條線上被裁掉一半;三是畫筆顏色取自 paint.getColor() 再於結束時還原,在這裡其實沒有必要——因為這個 Paint 是我們自己新建的,不影響別人。另外,用 getAttributeValue() 讀 textColor 若使用者沒設定會拿到 null,後續 Color.parseColor() 就會拋例外,正式環境建議加一層預設值保護。
運行結果:

源碼下載:Android UI 實現帶邊框的文本框
Android 網絡編程:用 GET 把數據送到伺服器(一)
Android 應用常需要與伺服器通訊,這就必須有伺服器端的配合。本文先談最基礎的 GET 方式。 GET 上傳數據適用於資料量不超過 2KB、對安全性要求不高的場景。
[Android] AIDL 是什麼?使用時機與實作範例完整解析
AIDL 是什麼 AIDL 全名為 Android Interface Definition Language,即 Android 介面定義語言,是 Android 為
Android 進程間通訊入門:Binder 機制概覽
Android 應用通常由多個 Activity 與 Service 組成,這些元件可能位於同一個行程,也可能分散在不同行程。當行程不同時,就需要依靠行程間通訊機制交換資
Android 成長之路(3):Activity 的生命週期入門
當用戶使用一個 App 時,Activity 會在不同狀態之間不斷切換。理解 Activity 的生命週期,是每一個 Android 開發者必須掌握的基礎能力。只有真正理