編輯:關於 Android 程式設計
PopupWindow 是一個可浮動在任何檢視之上的輕量容器,常用來做「點選後出現相簿/拍照」這類浮動選單,比 Dialog 更不會遮斷使用者操作。


先 inflate 彈出內容的版面,用它建立 PopupWindow 物件,觸發時呼叫 showAtLocation(),關閉時呼叫 dismiss()。
package sn.qdj.popupwindowdemo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
/**
* PopupWindow 使用
* @author qingdujun
*/
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* popup.xml 為彈出界面布局
*/
View root = getLayoutInflater().inflate(R.layout.popup, null);
/**
* 彈出界面
* 寬度:400
* 高度:200
*/
final PopupWindow popup = new PopupWindow(root, 400, 200);
Button btn = (Button)findViewById(R.id.btn);
Button close = (Button)findViewById(R.id.close);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/**
* 在指定位置彈出
*
* 第一個參數指定 PopupWindow 的錨點 view,即依附在哪個 view 上。
* 第二個參數指定起始點為 parent 的右下角
* 第三個參數設置以 btn 的下方為原點,向左、上各偏移 0 像素。
*/
popup.showAtLocation(findViewById(R.id.btn), Gravity.BOTTOM, 0, 0);
}
});
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
/**
* 關閉 PopupWindow
*/
popup.dismiss();
}
});
}
}
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="彈出" /> <Button android:id="@+id/close" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/btn" android:text="關閉" /> </RelativeLayout>
<?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:orientation="vertical" > <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="相冊" /> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍照" /> </LinearLayout>
第一,顯示時加上 setOutsideTouchable(true) 與 setFocusable(true),點擊外部才能自動關閉。第二,若要更精準地貼齊某個檢視,改用 showAsDropDown(anchorView, x, y) 會比 showAtLocation() 方便,它會自動依錨點位置計算座標並處理鍵盤遮擋。第三,若只是想顯示一個選單清單,直接考慮 ListPopupWindow 或 PopupMenu,能省下自己寫 Adapter 的功夫。
Android OpenGL ES 繪製 3D 圖形的核心步驟
OpenGL ES 是 OpenGL 三維圖形 API 的子集,專為手機、PDA 和遊戲主機等嵌入式設備設計。早期 Android 設備普遍支持 OpenGL ES 1.
Android 設計模式之組合模式:View 與 ViewGroup 的樹形世界
如果說哪個設計模式在 Android 裡最氾濫,答案幾乎毫無懸念:組合模式。UI 體系中的每一個 widget、每一個佈局,都建立在 View 與 ViewGroup 這
Android 拍照與相冊選圖,含圖片裁剪完整實作
幾乎每個有註冊功能的 App 都需要「設定頭像」。這個看似簡單的功能,背後其實牽涉到相機啟動、相冊挑選、URI 取得、以及裁剪後的結果回傳。今天就把這套常用模組完整記錄下
Android 實現 Tab 頁面的四種主流寫法全整理
Tab 樣式的界面幾乎是每個 App 的標配,而實現它的方式這些年也越來越多。本文把最常見的四種思路一次排開來比較,方便你依專案規模和維護成本來挑選。 傳統 ViewP