編輯:關於 Android 程式設計
先看成品:

只有一顆「退出系統」按鈕,用 RelativeLayout 靠頂對齊即可。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/skin_background">
<Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="退出系統"
android:textSize="20sp" />
</RelativeLayout>
三個 TextView 分別扮演標題、確認動作與取消動作,各自套用不同的九宮格背景,讓整體看起來像一個連體卡片。
<?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="wrap_content"
android:background="@color/transparent"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/actionsheet_top_normal"
android:gravity="center"
android:text="@string/title"
android:textColor="#8F8F8F"
android:textSize="16sp" />
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/actionsheet_bottom_selector"
android:gravity="center"
android:text="@string/content"
android:textColor="#FD4A2E"
android:textSize="16sp" />
<TextView
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/actionsheet_single_selector"
android:gravity="center"
android:text="@string/cancel"
android:textColor="#037BFF"
android:textSize="16sp" />
</LinearLayout>
用一個靜態方法把 Dialog 的細節包起來,對外只暴露一個回呼介面,呼叫端不必知道裡面怎麼運作。
public class ActionSheet {
public interface OnActionSheetSelected {
void onClick(int whichButton);
}
private ActionSheet() { }
public static Dialog showSheet(Context context,
final OnActionSheetSelected actionSheetSelected,
OnCancelListener cancelListener) {
final Dialog dlg = new Dialog(context, R.style.ActionSheet);
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)
inflater.inflate(R.layout.actionsheet, null);
// 讓彈窗盡量貼齊寬度
final int cFullFillWidth = 10000;
layout.setMinimumWidth(cFullFillWidth);
TextView mContent = (TextView) layout.findViewById(R.id.content);
TextView mCancel = (TextView) layout.findViewById(R.id.cancel);
mContent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
actionSheetSelected.onClick(0);
dlg.dismiss();
}
});
mCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
actionSheetSelected.onClick(1);
dlg.dismiss();
}
});
// 靠底部對齊
Window w = dlg.getWindow();
WindowManager.LayoutParams lp = w.getAttributes();
lp.x = 0;
final int cMakeBottom = 0;
lp.y = cMakeBottom;
lp.gravity = Gravity.CENTER;
dlg.onWindowAttributesChanged(lp);
dlg.setCanceledOnTouchOutside(false);
if (cancelListener != null) {
dlg.setOnCancelListener(cancelListener);
}
dlg.setContentView(layout);
dlg.show();
return dlg;
}
}
Activity 同時實作點擊與取消兩個回呼,並用 Toast 驗證結果。
public class MainActivity extends Activity
implements ActionSheet.OnActionSheetSelected, OnCancelListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.show).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ActionSheet.showSheet(MainActivity.this,
MainActivity.this, MainActivity.this);
}
});
}
@Override
public void onClick(int whichButton) {
switch (whichButton) {
case 0:
showToast("確定");
break;
case 1:
showToast("取消");
break;
default:
break;
}
}
@Override
public void onCancel(DialogInterface dialog) {
showToast("取消");
}
private void showToast(CharSequence charSequence) {
Toast.makeText(this, charSequence, Toast.LENGTH_SHORT).show();
}
}
關鍵點在於三處:setMinimumWidth 撐開寬度、WindowManager.LayoutParams 控制位置、以及自訂 style 去掉預設的 Dialog 標題與邊框。掌握這三點,就能做出與 iOS ActionSheet 相當接近的操作體驗。
破解 Android 設定中的隱藏彩蛋:隱藏項實現原理解析
做程式的人偶爾也會惡搞一下。Android 的設定裡就藏了這樣一個趣味彩蛋:連續快速點擊「Android 版本」,會冒出一張圖片;而在 4.0 系統上按住 Android
Android 佈局實作:用權重堆疊出九宮格式圖文入口
這個範例示範一種很常見的首頁樣式:畫面被切成若干等高的橫列,每一列再等寬切分為數格,每格放一張著色方塊與一行文字,點擊後跳出提示。整個效果完全靠 LinearLayout
Gradle for Android 學習筆記(一):從 gradlew 到變體與簽名配置
Gradle 是 Android 的官方建置系統,概念不算難,但設定檔的寫法相當自由,入門時容易迷失在各種寫法裡。這篇從最基礎的命令與任務概念開始,一路談到 buildT
Android 5.0 學習筆記:AnimatedVectorDrawable 入門
幾行程式碼加幾個設定檔,就能做出流程順暢、無縫銜接的向量動畫。 範例程式碼:animated-vector-drawable VectorDrawable 從 And