編輯:Android 開發實例
PopupWindow 能實現浮層效果,主要能力包括:可自定義 view(透過 LayoutInflater)、可設定出現與退出時的動畫、可指定顯示位置等。
為了把 PopupWindow 的多項功能一次展現,並力求用簡單的程式碼實現,本文實作了一個「點擊按鈕從左側彈出菜單」的功能,包含出現與退出的動畫效果,並在點擊其他區域時讓彈出層自動消失。
源碼:
1. PopwindowOnLeftActivity.java
代碼如下:
package com.pop.main;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.PopupWindow;
public class PopwindowOnLeftActivity extends Activity {
// 聲明PopupWindow對象的引用
private PopupWindow popupWindow;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 點擊按鈕彈出菜單
Button pop = (Button) findViewById(R.id.popBtn);
pop.setOnClickListener(popClick);
}
//點擊彈出左側菜單的顯示方式
OnClickListener popClick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
getPopupWindow();
// 這裡是位置顯示方式,在按鈕的左下角
popupWindow.showAsDropDown(v);
// 這裡可以嘗試其它效果方式,如popupWindow.showAsDropDown(v,
// (screenWidth-dialgoWidth)/2, 0);
// popupWindow.showAtLocation(findViewById(R.id.layout),
// Gravity.CENTER, 0, 0);
}
};
/**
* 創建PopupWindow
*/
protected void initPopuptWindow() {
// TODO Auto-generated method stub
// 獲取自定義布局文件pop.xml的視圖
View popupWindow_view = getLayoutInflater().inflate(R.layout.pop, null,
false);
// 創建PopupWindow實例,200,150分別是寬度和高度
popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);
// 設置動畫效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
//點擊其他地方消失
popupWindow_view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
// pop.xml視圖裡面的控件
Button open = (Button) popupWindow_view.findViewById(R.id.open);
Button save = (Button) popupWindow_view.findViewById(R.id.save);
Button close = (Button) popupWindow_view.findViewById(R.id.close);
// pop.xml視圖裡面的控件觸發的事件
// 打開
open.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這裡可以執行相關操作
System.out.println("打開操作");
// 對話框消失
popupWindow.dismiss();
}
});
// 保存
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這裡可以執行相關操作
System.out.println("保存操作");
popupWindow.dismiss();
}
});
// 關閉
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 這裡可以執行相關操作
System.out.println("關閉操作");
popupWindow.dismiss();
}
});
}
/**
* 獲取PopupWindow實例
*/
private void getPopupWindow() {
if (null != popupWindow) {
popupWindow.dismiss();
return;
} else {
initPopuptWindow();
}
}
}
主要界面
2. main.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/popBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/pop_left" />
</LinearLayout>
彈出層的佈局
3. pop.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray">
<Button android:id="@+id/open"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:text="@string/open"/>
<Button android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:text="@string/save"/>
<Button android:id="@+id/close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn"
android:text="@string/close"/>
</LinearLayout>
value 下的 style 檔
4. style
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AnimationFade">
<!-- PopupWindow左右彈出的效果-->
<item name="android:windowEnterAnimation">@anim/in_lefttoright</item>
<item name="android:windowExitAnimation">@anim/out_righttoleft</item>
</style>
</resources>
value 下的 string 檔
5. string.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PopwindowOnLeftActivity!</string>
<string name="app_name">PopwindowOnLeft</string>
<string name="pop_left">彈出左側菜單</string>
<string name="open">打開</string>
<string name="save">保存</string>
<string name="close">關閉</string>
</resources>
anim 目錄下的檔案
出現時從左往右的動畫檔
6. in_lefttoright.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 定義從左向右進入的動畫 -->
<translate
android:fromXDelta="-100%"
android:toXDelta="0"
android:duration="500"/>
</set>
退出時從右往左消失的動畫
7. out_righttoleft.xml
代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 定義從右向左動畫退出動畫 -->
<translate
android:fromXDelta="0"
android:toXDelta="-100%"
android:duration="500"/>
</set>
Eclipse 中 aapt.exe 停止工作的排查方式
問題現象 在 Eclipse 開發 Android 專案時,若版面配置或 XML 檔有錯誤,可能會突然出現 aapt.exe 停止工作的提示。這類錯誤不一定伴隨明確訊息,
Android MediaPlayer 影音播放開發指南
Android 提供了多種控制音訊與影片播放的方式,其中最常用的一種,就是透過名為 MediaPlayer 的類別。 Android 的 MediaPlayer 類別能存
Android JSON 教學:從原始字串到天氣資訊
JSON 解析是 Android 應用串接網路服務時常見的需求。本文從原始 JSON 字串出發,說明如何建立 JSONObject、讀取嵌套物件,並在畫面中呈現天氣資訊。
Android 應用本地化:讓 App 依地區自動切換語言
Android 應用會運行在name各語言環境的裝置上。要讓應用更具親和力,就必須讓文字、數字與檔案等资源能配合裝置的語言環境自動調整。本章將說明如何依不同區域(Loca