Android利用PopupWindow實現點擊工具欄彈出下拉菜單

jopen 10年前發布 | 36K 次閱讀 Android Android開發 移動開發

1.概述

本文將介紹如何利用PopupWindow實現點擊屏幕頂部工具欄按鈕彈出下拉菜單的功能。先上圖:

圖1圖2


圖3圖4


圖5圖6

2.代碼實現

首先是activity_main.xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#897D13"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/rl_topbar"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:background="#BF7F40"
        android:gravity="center_vertical" >

        <TextView
            android:id="@+id/tv_left"
            android:layout_width="70dip"
            android:layout_height="30dip"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dip"
            android:background="@drawable/btn_selector_pop"
            android:clickable="true"
            android:gravity="center"
            android:text="left"
            android:textColor="@android:color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_middle"
            android:layout_width="70dip"
            android:layout_height="30dip"
            android:layout_centerHorizontal="true"
            android:background="@drawable/btn_selector_pop"
            android:clickable="true"
            android:gravity="center"
            android:text="middle"
            android:textColor="@android:color/white"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tv_right"
            android:layout_width="70dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dip"
            android:background="@drawable/btn_selector_pop"
            android:clickable="true"
            android:gravity="center"
            android:text="right"
            android:textColor="@android:color/white"
            android:textSize="16sp" />
    </RelativeLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Drop-down Menu"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</RelativeLayout>
其中,rl_topbar定義了頂部工具欄,里面包含左中右三個TextView,點擊這三個控件會彈出相應的下拉菜單。

接下來是MainActivity.java文件:
package com.example.dropdownmenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

    // 工具欄
    private RelativeLayout rlTopBar;

    // 左中右三個控件(工具欄里)
    private TextView tvLeft;
    private TextView tvRight;
    private TextView tvMiddle;

    // 左中右三個彈出窗口
    private PopupWindow popLeft;
    private PopupWindow popRight;
    private PopupWindow popMiddle;

    // 左中右三個layout
    private View layoutLeft;
    private View layoutRight;
    private View layoutMiddle;

    // 左中右三個ListView控件(彈出窗口里)
    private ListView menulistLeft;
    private ListView menulistRight;
    private ListView menulistMiddle;

    // 菜單數據項
    private List<Map<String, String>> listLeft;
    private List<Map<String, String>> listRight;
    private List<Map<String, String>> listMiddle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initParam();
    }

    private void initParam() {
        rlTopBar = (RelativeLayout) this.findViewById(R.id.rl_topbar);

        tvLeft = (TextView) this.findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(myListener);
        // 初始化數據項
        listLeft = new ArrayList<Map<String, String>>();
        for (int i = 1; i < 10; i++) {
            HashMap<String, String> mapTemp = new HashMap<String, String>();
            mapTemp.put("item", "left " + i);
            listLeft.add(mapTemp);
        }

        tvRight = (TextView) this.findViewById(R.id.tv_right);
        tvRight.setOnClickListener(myListener);
        // 初始化數據項
        listRight = new ArrayList<Map<String, String>>();
        for (int i = 1; i < 10; i++) {
            HashMap<String, String> mapTemp = new HashMap<String, String>();
            mapTemp.put("item", "right " + i);
            listRight.add(mapTemp);
        }

        tvMiddle = (TextView) this.findViewById(R.id.tv_middle);
        tvMiddle.setOnClickListener(myListener);
        // 初始化數據項
        listMiddle = new ArrayList<Map<String, String>>();
        for (int i = 1; i < 10; i++) {
            HashMap<String, String> mapTemp = new HashMap<String, String>();
            mapTemp.put("item", "mid " + i);
            listMiddle.add(mapTemp);
        }
    }

    private View.OnClickListener myListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.tv_left:
                if (popLeft != null && popLeft.isShowing()) {
                    popLeft.dismiss();
                } else {
                    layoutLeft = getLayoutInflater().inflate(
                            R.layout.pop_menulist, null);
                    menulistLeft = (ListView) layoutLeft
                            .findViewById(R.id.menulist);
                    SimpleAdapter listAdapter = new SimpleAdapter(
                            MainActivity.this, listLeft, R.layout.pop_menuitem,
                            new String[] { "item" },
                            new int[] { R.id.menuitem });
                    menulistLeft.setAdapter(listAdapter);

                    // 點擊listview中item的處理
                    menulistLeft
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0,
                                        View arg1, int arg2, long arg3) {
                                    // 改變頂部對應TextView值
                                    String strItem = listLeft.get(arg2).get(
                                        "item");
                                    tvLeft.setText(strItem);

                                    // 隱藏彈出窗口
                                    if (popLeft != null && popLeft.isShowing()) {
                                        popLeft.dismiss();
                                    }
                                }
                            });

                    // 創建彈出窗口
                    // 窗口內容為layoutLeft,里面包含一個ListView
                    // 窗口寬度跟tvLeft一樣
                    popLeft = new PopupWindow(layoutLeft, tvLeft.getWidth(),
                            LayoutParams.WRAP_CONTENT);

                    ColorDrawable cd = new ColorDrawable(-0000);
                    popLeft.setBackgroundDrawable(cd);
                    popLeft.setAnimationStyle(R.style.PopupAnimation);
                    popLeft.update();
                    popLeft.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    popLeft.setTouchable(true); // 設置popupwindow可點擊
                    popLeft.setOutsideTouchable(true); // 設置popupwindow外部可點擊
                    popLeft.setFocusable(true); // 獲取焦點

                    // 設置popupwindow的位置(相對tvLeft的位置)
                    int topBarHeight = rlTopBar.getBottom();
                    popLeft.showAsDropDown(tvLeft, 0,
                            (topBarHeight - tvLeft.getHeight()) / 2);

                    popLeft.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            // 如果點擊了popupwindow的外部,popupwindow也會消失
                            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                                popLeft.dismiss();
                                return true;
                            }
                            return false;
                        }
                    });
                }
                break;
            case R.id.tv_right:
                if (popRight != null && popRight.isShowing()) {
                    popRight.dismiss();
                } else {
                    layoutRight = getLayoutInflater().inflate(
                            R.layout.pop_menulist, null);
                    menulistRight = (ListView) layoutRight
                            .findViewById(R.id.menulist);
                    SimpleAdapter listAdapter = new SimpleAdapter(
                            MainActivity.this, listRight, R.layout.pop_menuitem,
                            new String[] { "item" },
                            new int[] { R.id.menuitem });
                    menulistRight.setAdapter(listAdapter);

                    // 點擊listview中item的處理
                    menulistRight
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0,
                                        View arg1, int arg2, long arg3) {
                                    String strItem = listRight.get(arg2).get(
                                        "item");
                                    tvRight.setText(strItem);

                                    if (popRight != null && popRight.isShowing()) {
                                        popRight.dismiss();
                                    }
                                }
                            });

                    popRight = new PopupWindow(layoutRight, tvRight.getWidth(),
                            LayoutParams.WRAP_CONTENT);

                    ColorDrawable cd = new ColorDrawable(-0000);
                    popRight.setBackgroundDrawable(cd);
                    popRight.setAnimationStyle(R.style.PopupAnimation);
                    popRight.update();
                    popRight.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    popRight.setTouchable(true); // 設置popupwindow可點擊
                    popRight.setOutsideTouchable(true); // 設置popupwindow外部可點擊
                    popRight.setFocusable(true); // 獲取焦點

                    // 設置popupwindow的位置
                    int topBarHeight = rlTopBar.getBottom();
                    popRight.showAsDropDown(tvRight, 0,
                            (topBarHeight - tvRight.getHeight()) / 2);

                    popRight.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            // 如果點擊了popupwindow的外部,popupwindow也會消失
                            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                                popRight.dismiss();
                                return true;
                            }
                            return false;
                        }
                    });
                }
                break;
            case R.id.tv_middle:
                if (popMiddle != null && popMiddle.isShowing()) {
                    popMiddle.dismiss();
                } else {
                    layoutMiddle = getLayoutInflater().inflate(
                            R.layout.pop_menulist, null);
                    menulistMiddle = (ListView) layoutMiddle
                            .findViewById(R.id.menulist);
                    SimpleAdapter listAdapter = new SimpleAdapter(
                            MainActivity.this, listMiddle, R.layout.pop_menuitem,
                            new String[] { "item" },
                            new int[] { R.id.menuitem });
                    menulistMiddle.setAdapter(listAdapter);

                    // 點擊listview中item的處理
                    menulistMiddle
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                @Override
                                public void onItemClick(AdapterView<?> arg0,
                                        View arg1, int arg2, long arg3) {
                                    String strItem = listMiddle.get(arg2).get(
                                        "item");
                                    tvMiddle.setText(strItem);

                                    if (popMiddle != null && popMiddle.isShowing()) {
                                        popMiddle.dismiss();
                                    }
                                }
                            });

                    popMiddle = new PopupWindow(layoutMiddle, tvMiddle.getWidth(),
                            LayoutParams.WRAP_CONTENT);

                    ColorDrawable cd = new ColorDrawable(-0000);
                    popMiddle.setBackgroundDrawable(cd);
                    popMiddle.setAnimationStyle(R.style.PopupAnimation);
                    popMiddle.update();
                    popMiddle.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                    popMiddle.setTouchable(true); // 設置popupwindow可點擊
                    popMiddle.setOutsideTouchable(true); // 設置popupwindow外部可點擊
                    popMiddle.setFocusable(true); // 獲取焦點

                    // 設置popupwindow的位置
                    int topBarHeight = rlTopBar.getBottom();
                    popMiddle.showAsDropDown(tvMiddle, 0,
                            (topBarHeight - tvMiddle.getHeight()) / 2);

                    popMiddle.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            // 如果點擊了popupwindow的外部,popupwindow也會消失
                            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                                popMiddle.dismiss();
                                return true;
                            }
                            return false;
                        }
                    });
                }
                break;
            default:
                break;
            }
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

以上代碼有詳細的注釋,這里就不做過多解釋啦!原理比較簡單,點擊工具欄上的TextView會彈出PopupWindow,里面包含ListView,顯示所有菜單項。

彈出的菜單窗口(PopupWindow)布局文件為pop_menulist.xml,就一個ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/shape_bg_pop"
    android:orientation="vertical"
    >
    <ListView
        android:id="@+id/menulist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#696969"
        android:dividerHeight="1.0dip"
        android:padding="2dip"
        android:scrollbars="none" >
    </ListView>
</LinearLayout>

而每個菜單項對應的布局文件為pop_menuitem.xml,只包含一個TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@drawable/bg_list_item_pop" >
    <TextView
        android:id="@+id/menuitem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:padding="10dip"
        android:textSize="16sp" />
</LinearLayout>

其他還有一些屬性資源相關的文件,不是很重要,大家可以下載工程源碼進行研究!

3.工程源碼




來自:http://blog.csdn.net/chadeltu/article/details/43487551

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!