Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 PopupWindow 實作點擊頂部按鈕彈出下拉選單

編輯:關於 Android 程式設計

這個效果技術門檻不高,核心就是 PopupWindow:點一下按鈕,彈出一個自訂的 View,View 內部可以自由佈置,最常見的應用是放一個 ListView 當選單。本文示範一個簡易版:點擊頂端按鈕後向下展開選單,並加上淡入淡出的動畫。

MainActivity.java

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);
    }
}

activity_main.xml

<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>

自訂 View 的版面配置(popview_item.xml)

<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 定位。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved