編輯:關於 Android 程式設計
剛接手公司的 Android 專案,設計稿丟來的需求是這樣:

彈出視窗四角要圓角,標題欄顏色與下方內容區不同,中間還要用藍色線分隔。
搜一輪之後,主流方案都是在 drawable 資料夾底下放一個 shape 檔,用元素描述形狀。之所以要放 drawable,以及各元素的詳細語法,可以參考 Android 官方文件或資深前輩的講解(例如 https://blog.csdn.net/lonelyroamer/article/details/8254592 寫得很細)。
shape 的價值在於它提供了 solid、gradient、stroke、padding、corners 這組元素,其中 corners 能分別控制四個角的半徑。這正是圓角視窗需要的:
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填滿色 -->
<solid android:color="#0000FF" />
<!-- 四角弧形;未指定的角預設半徑為 0 -->
<corners
android:topLeftRadius="@dimen/rounded_amplitude"
android:topRightRadius="@dimen/rounded_amplitude" />
<gradient
android:angle="270"
android:centerColor="#0000FF"
android:endColor="#0000FF"
android:startColor="#0000FF" />
</shape>
要特別注意的是:corners 只寫設定的那幾個角會生效,沒寫的角預設半徑為 0(也就是直角)。這一點上機跑一次就驗證得了。
定義好之後,在任何 Layout、View、TextView 上加上 android:background="@drawable/shapeyuanjiao3" 就能套用,也能只讓特定某個角圓潤。
先看雛形結構:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.pop.main.clipimageview
android:id="@+id/front_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@drawable/shapeyuanjiao"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="30dip"
android:background="@drawable/shapeyuanjiao3"
android:gravity="center"
android:text="保證金狀態"
android:textColor="@color/white" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@color/blue" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dip"
android:text="帳號 09102"
android:textColor="@color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dip"
android:text="浮動損益"
android:textColor="@color/black" />
</LinearLayout>
</RelativeLayout>
關鍵在這一段:標題的 TextView 單獨套一個藍色 shape,整個視窗外框套另一個灰色 shape。兩個檔案的差異只在圓角涵蓋的角數與填色,複製一份改兩行即可。
外框那層長這樣:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:background="@drawable/shapeyuanjiao"
android:orientation="vertical">
實際執行結果:

界面確實樸素,別嫌醜。過程繞了不少路,我把兩個坑列出來給同樣卡住的人參考。
網路上還有一種做法,用 shape 的 padding 搭配 stroke 撐出邊距來營造圓角:
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#0033FF" />
<!-- 用邊距「假裝」圓角 -->
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
<stroke
android:width="1dp"
android:color="#CdCdCd" />
<corners android:radius="10dp" />
</shape>
這種寫法會讓整個視窗看起來像被額外包了一層,視覺邊界很彆扭,不符合這次的需求。有需要的可以參考,但別期待太好。

一開始我的想法很直覺:標題要藍底,那就 android:background="@color/blue";外層 LinearLayout 套 shape 圓角。結果跑起來——一點圓角都沒有。
問了同事才恍然大悟:外層 Layout 雖然設了圓角 shape,但每一列子 View 一旦各自設了背景色,就會把外框的樣式整片蓋掉,圓角自然被埋在底下看不見。
解法很乾脆:子 View 不要設背景色,只讓標題那一列單獨套一個藍色的 shape,其餘保持透明。這樣外框的圓角才有機會露出來。
Android 圖形動畫(二)以程式碼實作漸變、縮放、位移與旋轉
Android 平台提供了兩類動畫:一類是 Tween 動畫,對場景中的物件連續做外觀變化來產生動畫效果(旋轉、平移、縮放、漸變);另一類是 Frame 動畫,也就是逐幀
基於XMPP協議打造Android即時通訊:IM聊天與多人聊天室全解析
由於項目需要一款基於XMPP協議的Android通訊軟件,於是便有了這次對XMPP的深入研究。本文從協議基礎出發,一路講到Smack的IM聊天與MultiUserChat
Android 四大組件之 Service:從啟動、綁定到生命週期
Service 是一個可以在後台執行長時間操作、且不提供用戶界面的應用組件。它可以由其他組件啟動,即使用戶切換到別的應用,它依然在後台繼續運行;組件也可以綁定到服務進行交
Android Toast 提示元件入門:使用方式、自訂版面與顯示效果
在 Android 中,Toast 是一種輕量級提示元件。它不會搶走使用者焦點,也不會打斷目前操作,常用來顯示短暫的成功、失敗或提醒訊息。Toast 的基本用法最常見的做