編輯:關於 Android 程式設計
Shape 是在 XML 中定義的一類 Drawable,用來快速做出圓角、漸層、邊框、實心填色等美觀的界面元素,非常適合 Android 的 UI 美化。
檔案位置:res/drawable/filename.xml,此檔名即為資源 ID。編譯後的資源型別為 GradientDrawable 的資源參照。在 Java 中用 R.drawable.filename 引用,在 XML 中用 @[package:]drawable/filename 引用。
語法範式如下:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="https://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
</shape>
為形狀建立圓角,僅適用於長方形。可用 android:radius 設定四角統一圓角,或用 topLeftRadius、topRightRadius、bottomLeftRadius、bottomRightRadius 分別設定。注意:每個角初始都必須給大於 1 的半徑,否則不會有圓角;若某些角不想圓滑,可先用 android:radius 設一個大於 1 的預設值,再逐一覆寫為 0dp。另外要特別小心——bottomLeftRadius 指的是「右下角」,bottomRightRadius 才是「左下角」,別記反了。
指定形狀的漸層填色。startColor 與 endColor 為起始與結束顏色;android:angle 為漸層角度,必須是 45 的整數倍。預設模式 android:type="linear"(線性漸層),也可設為 "radial"(徑向漸層,需指定 android:gradientRadius)或 "sweep"。關於 angle 的實際行為,經實驗:angle=0 由左到右、90 由上到下、180 由右到左、360 與 0 相同——可理解為把控制項內部當成繞中心旋轉的色板,angle 即為旋轉角度(須為 45 的倍數)。
用 android:color 指定填滿形狀的單一顏色。
用 android:width 設定描邊寬度、android:color 設定顏色。若要虛線,可加上 android:dashWidth(每段實線的寬度)與 android:dashGap(段與段之間的間隔)。
定義內容離邊界的距離,效果等同 android:padding_left、android:padding_right 等屬性,填的是 View 內容的位置而非形狀本身。
XML 檔存於 res/drawable/gradient_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="45"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
在布局中套用為 View 背景:
<TextView
android:background="@drawable/gradient_box"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
或用程式碼套用:
Resources res = getResources(); Drawable shape = res.getDrawable(R.drawable.gradient_box); TextView tv = (TextView) findViewByID(R.id.textview); tv.setBackground(shape);
掌握以上元素,就能用純 XML 輕鬆做出圓角、漸層、邊框等各式美觀的控制項背景。
自訂 AlertDialog 對話框:做一個有 username / password 欄位的登入視窗
Android 開發中經常需要在畫面上彈一個對話框,例如詢問使用者、讓使用者做選擇,這類元件統稱為 Dialog,而 AlertDialog 採用的是建造者模式(Buil
Android 手機截屏的幾條路:從 DrawingCache 到 MediaProjection
一開始以為截屏是件輕鬆事:在 Activity 上放個按鈕,點一下就截圖存進手機。動手前照慣例翻書、查資料,結果知道得越多,越覺得事情不對勁。 多數講解 Android
Android 高效編碼:推送原理、AndroidPn 與 JPush 實作
推送是許多 Android 應用的重要功能,例如即時通知、聊天訊息與行銷訊息。本文從推送原理出發,介紹 AndroidPn 與 JPush 的整合方式,並說明自定義推送的
Android 移植印表機驅動的三階段調試筆記(以 CUPS 為核心)
印表機嚴格說來跟 Android 沒什麼直接關係,它真正緊密相依的是 Linux 核心與使用者層的列印堆疊。最近在調試印表機驅動,累積了一些心得,記錄在此。 為什麼要分