編輯:關於 Android 程式設計
卡片式設計在行動應用裡無所不在,而「圓角矩形」就是最基本的卡片外觀。做法很簡單:在 drawable 目錄建立一份 XML,再用 android:background 引用它。
在 res/drawable-mdpi/(或直接放在 res/drawable/)建立 corners_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 填充顏色 -->
<solid android:color="#FFFFFF" />
<!-- 四個角的圓弧半徑 -->
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp" />
</shape>
一個容易搞錯的細節:這裡的命名容易讓人誤解——bottomRightRadius 對應的是左下角,而 bottomLeftRadius 對應的是右下角。原因是這組名稱沿用「以自身為原點、向右向下為正」的座標思維,字面順序與視覺位置剛好相反。若不確定,逐一改值實驗一次最準。
如果四個角半徑相同,可以簡化成一寫:
<corners android:radius="5dp" />
注意:不能同時使用 radius 與個別角的屬性,後者會被忽略。
android:background="@drawable/corners_bg"
副檔名不寫,直接使用檔名。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E1E0DE"
android:orientation="vertical" >
<!-- 標題列 -->
<TextView
android:id="@+id/titleText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#2B3439"
android:gravity="center"
android:text="發現"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<!-- 圓角卡片:單行 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
android:background="@drawable/corners_bg"
android:gravity="center_vertical"
android:padding="10dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/find_more_friend_photograph_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="朋友圈"
android:textColor="#000"
android:textSize="18sp" />
</LinearLayout>
<!-- 圓角卡片:雙行 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="101dp"
android:layout_margin="10dp"
android:background="@drawable/corners_bg"
android:orientation="vertical"
android:padding="10dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/find_more_friend_scan" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="掃一掃"
android:textColor="#000"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/come_from_shake" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="14dp"
android:text="搖一搖"
android:textColor="#000"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<solid>:純色填充。<gradient>:漸層,可用 android:angle 控制方向(必須為 45 的倍數)。<stroke>:邊框。加上 android:width 與 android:color 就有描邊;再給 dashWidth 與 dashGap 可做虛線。<corners>:圓角。<padding>:內距,會影響子元件與邊界的距離。<size>:指定寬高,常用於 selector 中的分隔線。把「圓角 + 描邊 + 漸層」疊在一起,就能做出接近 Material Card 的視覺:
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#FFFFFF"
android:endColor="#F5F5F5"
android:angle="90" />
<stroke android:width="1dp" android:color="#DDDDDD" />
<corners android:radius="8dp" />
</shape>
若想加上按壓回饋,再包一層 selector,分別指向正常與 state_pressed 兩個 shape 檔即可。
新專案可直接使用 MaterialCardView,透過 app:cardCornerRadius、app:cardElevation 與 app:strokeColor 設定外觀,並自動獲得陰影與漣漪效果,不必維護多份 drawable XML。
Android 動作視圖與動作提供器實戰
在 Android 的應用列或 Toolbar 中,動作視圖與動作提供器能讓使用者更直覺地操作功能。動作視圖適合嵌入搜尋框、輸入欄等元件;動作提供器則適合用來觸發分享、選
OpenGL ES 零基礎系列(三):渲染管線與 VertexShader、FragmentShader
前言 前面兩篇一直在提「著色器」(Shader),第二篇也明確說過:著色器只能用於 OpenGL ES 2.x 之後的可程式化管線,1.x 是不支援的。但我們始終沒解釋為
Android 小功能實作:內部檔案的讀與寫
一、布局 先看效果圖: main.xml: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmln
Android Canvas 練習(10):純手繪南丁格爾玫瑰圖
第一次在圖表作品集裡看到南丁格爾玫瑰圖時,我的反應是「這也太漂亮了」,但老實說完全沒看懂在表達什麼。查了資料才知道,這張圖的背後是弗羅倫斯・南丁格爾——現代護理的奠基者,