編輯:關於 Android 程式設計
PopupWindow 位於 android.widget 套件下,以彈出視窗的形式呈現內容。官方文件對它的描述是:「一個彈出視窗控制項,可以用來顯示任意的 View,並浮動在目前的 Activity 之上」。因為它能承載任何 View,所以自訂 menu、AlertDialog 風格的彈窗,都可以用它來實作。
使用流程很簡單:在 onCreate() 中初始化,之後透過觸發事件把它顯示出來。
public class ShowPopupWindow extends Activity implements View.OnClickListener {
View view;
PopupWindow pop;
Button btnShowAsDrawDown;
Button btnShowAsDrawDown1;
Button btnShowAtLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup_activity);
btnShowAsDrawDown = (Button) findViewById(R.id.btnShowAsDrawDown);
btnShowAsDrawDown.setOnClickListener(this);
btnShowAsDrawDown1 = (Button) findViewById(R.id.btnShowAsDrawDown1);
btnShowAsDrawDown1.setOnClickListener(this);
btnShowAtLocation = (Button) findViewById(R.id.btnShowAt);
btnShowAtLocation.setOnClickListener(this);
initPopupWindow();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnShowAsDrawDown:
if (pop.isShowing()) {
pop.dismiss();
} else {
pop.showAsDropDown(v);
}
break;
case R.id.btnShowAsDrawDown1:
if (pop.isShowing()) {
pop.dismiss();
} else {
pop.showAsDropDown(v, 0, -160);
}
break;
default:
if (pop.isShowing()) {
pop.dismiss();
} else {
pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 0, 0);
}
break;
}
}
private void initPopupWindow() {
view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
pop = new PopupWindow(view,
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setOutsideTouchable(true);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pop.dismiss();
}
});
}
}
三個按鈕分別示範了兩種定位方式:showAsDropDown() 相對於觸發它的那個 View 顯示(可再帶 x、y 偏移量微調位置),showAtLocation() 則是相對於整個視窗以 Gravity 對齊。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="pop demo!" />
<Button
android:id="@+id/btnShowAsDrawDown"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Show as drawndown(one parameter)" />
<Button
android:id="@+id/btnShowAsDrawDown1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Show as drawndown(three parameters)" />
<Button
android:id="@+id/btnShowAt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Show At Location" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:background="#d3d3d3"
android:gravity="center_horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_pop"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_width="wrap_content"
android:text="Close" />
</LinearLayout>
幾個實務上值得留意的點:setOutsideTouchable(true) 讓使用者點擊彈窗外區域時可以互動,配合在 popup 內容的 View 上掛 setOnClickListener() 呼叫 dismiss(),就能做出「點任何地方關閉」的效果。另外記得用 isShowing() 判斷狀態,避免重複彈出。
Android 仿微信錄製音頻並發送的完整實現
MyRecorder(仿微信,錄製音頻並發送功能)① 布局實現(activity_main.xml)整體採用線性布局,上方是一個 ListView,下方是一個自定義 Bu
用 ListView 打造自定義表格(三):加入列合併能力
在前兩篇的基礎上,我把自定義表格又往前推了一步——現在它支援「列合併」了。整體架構拆成兩種模式:簡單模式與複雜模式,開發者可依場景自由挑選。 兩種模式的分野 簡單模式:
Android 自訂 View:仿攜程首頁的點擊縮放 ImageView
最近下了個攜程 App,點開首頁時注意到,它的按鈕在點擊時並不是常見的變色效果,而是先微微收縮,放開後再彈回原本的大小。這個效果雖然小,卻讓人覺得很新穎,於是便決定模仿一
Android 資源圖片讀取機制:drawable 目錄、密度與縮放規則
為何會有多個 drawable 目錄 Android 專案建立時,res 下常會出現 drawable-ldpi、drawable-mdpi、drawable-hdpi