編輯:關於 Android 程式設計
這個效果技術門檻不高,核心就是 PopupWindow:點一下按鈕,彈出一個自訂的 View,View 內部可以自由佈置,最常見的應用是放一個 ListView 當選單。本文示範一個簡易版:點擊頂端按鈕後向下展開選單,並加上淡入淡出的動畫。
public class MainActivity extends Activity implements OnClickListener {
private PopupWindow popupwindow;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// 已展開則收合
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
return;
} else {
initmPopupWindowView();
// showAsDropDown:相對於觸發元件,向下展開
popupwindow.showAsDropDown(v, 0, 5);
}
break;
default:
break;
}
}
public void initmPopupWindowView() {
// 載入自訂版面配置 popview_item.xml
View customView = getLayoutInflater().inflate(R.layout.popview_item, null, false);
// 建立 PopupWindow 實例,250、280 為寬與高
popupwindow = new PopupWindow(customView, 250, 280);
// 套用動畫樣式(R.style.AnimationFade 為自行定義)
popupwindow.setAnimationStyle(R.style.AnimationFade);
// 點擊外部區域時收合
customView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popupwindow != null && popupwindow.isShowing()) {
popupwindow.dismiss();
popupwindow = null;
}
return false;
}
});
// 選單項目
Button btton2 = (Button) customView.findViewById(R.id.button2);
Button btton3 = (Button) customView.findViewById(R.id.button3);
Button btton4 = (Button) customView.findViewById(R.id.button4);
btton2.setOnClickListener(this);
btton3.setOnClickListener(this);
btton4.setOnClickListener(this);
}
}
<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"
android:background="#000000"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:background="#C0C0C0"
android:text="點擊下拉列表" />
</RelativeLayout>
<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"
android:background="#C0C0C0" >
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingRight="70dp"
android:text="viviens" />
<Button
android:id="@+id/button3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:paddingRight="70dp"
android:text="mryang" />
<Button
android:id="@+id/button4"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button3"
android:paddingRight="70dp"
android:text="張曉達" />
</RelativeLayout>
進入畫面(anim/inuptodown.xml):
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="-100%"
android:toYDelta="0" />
</set>
離開畫面(anim/outdowntoup.xml):
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="-100%" />
</set>
樣式定義(styles.xml):
<style name="AnimationFade">
<!-- PopupWindow 的進出動畫 -->
<item name="android:windowEnterAnimation">@anim/inuptodown</item>
<item name="android:windowExitAnimation">@anim/outdowntoup</item>
</style>
成果如下:

幾個實務提醒:showAsDropDown() 會自動依可用空間決定向上或向下展開;若選單內容較多,建議把自訂 View 改成 ListView 並搭配 Adapter,同時加上 setOutsideTouchable(true) 讓點擊外部能順利關閉。若要 Відповідь 更完整的 PopupWindow 行為,也可考慮 showAtLocation() 搭配 Gravity 定位。
Fragment 與 Activity 的通信方式及 Fragment 生命週期詳解
一、Fragment 與 Activity 的通信 最推薦的做法是「接口回呼」,五步搞定: 在 Fragment 中聲明一個接口 在 Activity 中實現該接口 在
用 Android Animation 做一個 loading 動畫
Animation 的用法門檻不高,這裡用一個 loading 效果把四種動畫都串一遍。 先看效果: Android 的 Animation 體系提供四種基本動畫: A
Android 通訊錄開發實戰(二):讀取並呈現通話記錄
在上一篇我們已經完成了聯繫人資料的讀取,本篇接著談談如何取得並展示手機的通話記錄。整體思路與聯繫人類似,透過系統的 CallLog 內容提供者查詢資料,再用自訂的 Ada
Android Espresso UI 測試完全入門
前言 Espresso 是 Google 官方推出的 Android 界面測試框架,它能夠讓我們以接近「用戶真實操作」的方式去驅動和驗證 App 的 UI。本文整理自官方