Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android利用PopupWindow實現點擊工具欄彈出下拉菜單

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

編輯:關於Android編程

1.概述

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

圖1圖2

 

圖3圖4

 

圖5圖6

2.代碼實現

首先是activity_main.xml布局文件:


    

        

        

        
    

    

其中,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> listLeft;
	private List> listRight;
	private List> 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>();
		for (int i = 1; i < 10; i++) {
			HashMap mapTemp = new HashMap();
			mapTemp.put(item, left  + i);
			listLeft.add(mapTemp);
		}

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

		tvMiddle = (TextView) this.findViewById(R.id.tv_middle);
		tvMiddle.setOnClickListener(myListener);
		// 初始化數據項
		listMiddle = new ArrayList>();
		for (int i = 1; i < 10; i++) {
			HashMap mapTemp = new HashMap();
			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


    
    

而每個菜單項對應的布局文件為pop_menuitem.xml,只包含一個TextView


    

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

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved