編輯:關於 Android 程式設計
Android 的彈窗做法有好幾種。最簡單的是直接叫一個 AlertDialog,幾行程式碼就能跳出一個方方正正的對話框;但若想做出「從畫面底部滑出、背景半透明、點擊外部可關閉」這種更貼近 App 品牌調性的效果,PopupWindow 會是更合適的工具。就實用性與自由度而言,這條路值得一試。
public void showDialog() {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage(getResources().getString(R.string.exit_confirm))
.setPositiveButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setNegativeButton("確定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 在這裡執行確定後的業務邏輯
}
})
.create();
dialog.show();
}
功能齊全,但樣式幾乎無法客製。接著看自定義版本能做到什麼程度:


<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:gravity="center"
android:text="@string/select_head"
android:textColor="#aaaaaa" />
<Button
android:id="@+id/takephoto"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:background="@null"
android:text="@string/takephoto"
android:textColor="#fc4643" />
<Button
android:id="@+id/selectfromalbum"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:background="@null"
android:text="@string/takefromalbum"
android:textColor="#fc4643" />
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="40.5dip"
android:layout_marginLeft="7.2dip"
android:layout_marginRight="7.2dip"
android:layout_marginTop="7.2dip"
android:background="@null"
android:text="@string/cancel"
android:textColor="#fc4643" />
</LinearLayout>
</RelativeLayout>
public class SelectPopupWindow extends PopupWindow {
Button takePhoto;
Button selectFromAlbum;
Button cancel;
View menuView;
public SelectPopupWindow(Context ctx, OnClickListener onClickListener) {
LayoutInflater inflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
menuView = inflater.inflate(R.layout.modify_head, null);
takePhoto = (Button) menuView.findViewById(R.id.takephoto);
takePhoto.setOnClickListener(onClickListener);
selectFromAlbum = (Button) menuView.findViewById(R.id.selectfromalbum);
selectFromAlbum.setOnClickListener(onClickListener);
cancel = (Button) menuView.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
setContentView(menuView);
setWidth(LayoutParams.MATCH_PARENT);
setHeight(LayoutParams.WRAP_CONTENT);
setFocusable(true);
// setAnimationStyle(R.style.AnimBottom); // 需要時再打開進場動畫
// 點擊彈窗窗外的區域時關閉
menuView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int height = menuView.findViewById(R.id.top).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP && y < height) {
dismiss();
}
return true;
}
});
}
}
public class TestPopupWindow extends Activity {
private SelectPopupWindow popupWindow;
private ImageView image;
private Button btn, btn1;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
init();
}
private void init() {
image = (ImageView) findViewById(R.id.image);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(onClickListener);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
test();
break;
case R.id.btn1:
startActivity(new Intent(TestPopupWindow.this, TestGsonActivity.class));
break;
case R.id.takephoto:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, 100);
break;
case R.id.selectfromalbum:
Intent intent1 = new Intent(Intent.ACTION_PICK);
intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 200);
break;
}
}
};
public void test() {
popupWindow = new SelectPopupWindow(this, onClickListener);
popupWindow.showAtLocation(findViewById(R.id.main),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
}
}
幾個實務提醒:setFocusable(true) 讓彈窗可以接收點擊事件,但也會順便吃掉返回鍵,記得處理;若要背景變暗,可在呼叫前對 Activity 的 Window 設定 alpha;showAtLocation 的 Gravity 決定彈窗出現的位置,搭配 setAnimationStyle 就能做出由下滑入的過場。
Android Loader 加載器完整解析:從啟動到 CursorLoader 實作
Loader 是什麼 Android 3.0(API 11)引入了 Loader 機制,目的是把「異步取資料」這件事從 Activity 的生命週期裡抽離出來。在 Loa
OpenCV 使用 Harris 演算法實作角點檢測
效果展示 下面先觀察 Harris 角點檢測在圖像上的實際輸出。角點通常會以圓點標記,越明顯的交會區域,響應值越高。 什麼是 Harris 角點檢測 角點可以理解為兩
Android 鍵盤事件監控原理解析:從輸入事件到安全防護
鍵盤監控的基本概念所謂鍵盤監控,通常是指在系統或應用運行期間,對使用者的按鍵、觸控與手勢等輸入事件進行記錄與分析。這類技術在除錯、自動化測試與安全研究中有其用途,但也涉及
Android 經典主界面框架(一):仿 QQ 底部 Tab 切換(Fragment 實現,附源碼)
反覆研究日常必用的幾款 Android App,從主界面的交互方式切入,大致可以歸成三類。本文記錄第一種,也是最經典的一種:底部若干 tab、頂部標題欄,tab 之間不支