Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
點(diǎn)擊下載該實(shí)例:
一、運(yùn)行截圖:
二、實(shí)現(xiàn)要點(diǎn):
(1)屏蔽系統(tǒng)彈出的菜單:
1、首先創(chuàng)建至少一個(gè)系統(tǒng)的菜單選項(xiàng)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
2、在onMenuOpened方法里顯示自己的菜單視圖,并返回FALSE。
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
myMenu.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM, 0,0);
return false; // true--顯示系統(tǒng)自帶菜單;false--不顯示。
}
(2)點(diǎn)擊菜單欄,切換菜單視圖時(shí),只要重新設(shè)置當(dāng)前的適配器對象就可以。
gv_body.setAdapter(bodyAdapter[arg2]); //改變選項(xiàng)視圖
(3)繼承PopupWindow,重寫一個(gè)類實(shí)現(xiàn)彈出對話框,主要是為了更好更簡便實(shí)現(xiàn)彈出菜單的樣式和事件響應(yīng)等等。
public class MyDefinedMenu extends PopupWindow { 。。。}
三、 具體代碼如下:
(1)布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
(2)程序代碼
1、主類:MyMenu
package com.myandroid.test;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MyMenu extends Activity {
private List<String> titles; //標(biāo)題欄
private List<List<String>> item_names; //選項(xiàng)名稱
private List<List<Integer>> item_images; //選項(xiàng)圖標(biāo)
private MyDefinedMenu myMenu; //彈出菜單
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//彈出菜單標(biāo)題欄
titles = addItems(new String[]{"菜單一", "菜單二", "菜單三"});
//選項(xiàng)圖標(biāo)
item_images = new ArrayList<List<Integer>>();
item_images.add(addItems(new Integer[]{R.drawable.bag,
R.drawable.bluetooth, R.drawable.earth, R.drawable.email}));
item_images.add(addItems(new Integer[]{R.drawable.map,
R.drawable.news, R.drawable.reader, R.drawable.sound, R.drawable.tape}));
item_images.add( addItems(new Integer[]{R.drawable.telephone,
R.drawable.bluetooth, R.drawable.earth, R.drawable.email}));
//選項(xiàng)名稱
item_names = new ArrayList<List<String>>();
item_names.add(addItems(new String[]{"購物", "藍(lán)牙", "游覽器", "郵件"}));
item_names.add(addItems(new String[]{"地圖", "新聞", "閱讀器", "音箱", "錄音"}));
item_names.add(addItems(new String[]{"電話", "藍(lán)牙", "閱讀器", "郵箱"}));
//創(chuàng)建彈出菜單對象
myMenu = new MyDefinedMenu(this, titles, item_names,
item_images, new ItemClickEvent());
}
/**
* 轉(zhuǎn)換為List<String>
* @param values
* @return
*/
private List<String> addItems(String[] values) {
List<String> list = new ArrayList<String>();
for (String var : values) {
list.add(var);
}
return list;
}
/**
* 轉(zhuǎn)換為List<Integer>
* @param values
* @return
*/
private List<Integer> addItems(Integer[] values) {
List<Integer> list = new ArrayList<Integer>();
for (Integer var : values) {
list.add(var);
}
return list;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
myMenu.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM, 0,0);
return false; // true--顯示系統(tǒng)自帶菜單;false--不顯示。
}
/**
* 菜單選項(xiàng)點(diǎn)擊事件
* @author Kobi
*
*/
class ItemClickEvent implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//顯示點(diǎn)擊的是哪個(gè)菜單哪個(gè)選項(xiàng)。
Toast.makeText(MyMenu.this, "Menu: " +
titles.get(myMenu.getTitleIndex()) +
" Item: " + item_names.get(myMenu.getTitleIndex()).get(arg2),
Toast.LENGTH_SHORT).show();
myMenu.dismiss(); //菜單消失
}
}
}
2、彈出菜單類:MyDefinedMenu、
package com.myandroid.test;
import java.util.List;
import com.myandroid.test.MyMenu.ItemClickEvent;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MyDefinedMenu extends PopupWindow {
private LinearLayout layout; //總的布局
private GridView gv_title; //菜單欄
private GridView gv_body; //選項(xiàng)視圖
private BodyAdatper[] bodyAdapter; //選項(xiàng)適配器
private TitleAdatper titleAdapter; //標(biāo)題適配器
private Context context; //上下文
private int titleIndex; //菜單序號
public MyDefinedMenu(Context context, List<String> titles,
List<List<String>> item_names, List<List<Integer>> item_images,
ItemClickEvent itemClickEvent) {
super(context);
this.context = context;
//布局框架
layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//菜單欄
titleIndex = 0;
gv_title = new GridView(context);
titleAdapter = new TitleAdatper(context, titles);
gv_title.setAdapter(titleAdapter);
gv_title.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
gv_title.setNumColumns(titles.size()); //菜單個(gè)數(shù)
gv_title.setBackgroundColor(Color.WHITE);
//選項(xiàng)視圖
bodyAdapter = new BodyAdatper[item_names.size()]; //各個(gè)視圖適配器
for (int i = 0; i < item_names.size(); i++) {
bodyAdapter[i] = new BodyAdatper(context, item_names.get(i), item_images.get(i));
}
gv_body = new GridView(context);
gv_body.setNumColumns(4); //每行顯示4個(gè)選項(xiàng)
gv_body.setBackgroundColor(Color.TRANSPARENT);
gv_body.setAdapter(bodyAdapter[0]); //設(shè)置適配器
//菜單項(xiàng)切換
gv_title.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
titleIndex = arg2; //記錄當(dāng)前選中菜單項(xiàng)序號
titleAdapter.setFocus(arg2);
gv_body.setAdapter(bodyAdapter[arg2]); //改變選項(xiàng)視圖
}
});
//設(shè)置選項(xiàng)點(diǎn)擊事件
gv_body.setOnItemClickListener(itemClickEvent);
//添加標(biāo)題欄和選項(xiàng)
layout.addView(gv_title);
layout.addView(gv_body);
// 添加菜單視圖
this.setContentView(layout);
this.setWidth(LayoutParams.FILL_PARENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);// menu菜單獲得焦點(diǎn) 如果沒有獲得焦點(diǎn)menu菜單中的控件事件無法響應(yīng)
}
/**
* 獲取當(dāng)前選中菜單項(xiàng)
* @return 菜單項(xiàng)序號
*/
public int getTitleIndex() {
return titleIndex;
}
}
3、菜單欄適配器:TitleAdatper
package com.myandroid.test;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class TitleAdatper extends BaseAdapter {
private List<String> titles;
private Context context;
private final TextView[] tv_titels;
public TitleAdatper(Context context, List<String> titles) {
this.context = context;
this.titles = titles;
tv_titels = new TextView[titles.size()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return titles.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/**
* 選中后,改變菜單顏色。
* @param position
*/
public void setFocus(int position) {
for (int i = 0; i < titles.size(); i++) {
tv_titels[i].setBackgroundColor(Color.WHITE);
}
tv_titels[position].setBackgroundColor(Color.BLUE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//菜單欄文字項(xiàng)
tv_titels[position] = new TextView(context);
tv_titels[position].setGravity(Gravity.CENTER);
tv_titels[position].setText(titles.get(position));
tv_titels[position].setTextSize(18);
tv_titels[position].setLayoutParams(new GridView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
return tv_titels[position];
}
}
4、菜單項(xiàng)視圖適配器:BodyAdatper
package com.myandroid.test;
import java.util.List;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BodyAdatper extends BaseAdapter {
private List<String> item_names;
private List<Integer> item_images;
private Context context;
public BodyAdatper(Context context, List<String> item_names,
List<Integer> item_images) {
this.context = context;
this.item_names = item_names;
this.item_images = item_images;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return item_images.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//總布局
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER);
//選項(xiàng)名稱
TextView tv_item = new TextView(context);
tv_item.setGravity(Gravity.CENTER);
tv_item.setLayoutParams(new GridView.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv_item.setText(item_names.get(position));
//選項(xiàng)圖表
ImageView img_item = new ImageView(context);
img_item.setLayoutParams(new LayoutParams(50, 50));
img_item.setImageResource(item_images.get(position));
//添加選項(xiàng)圖標(biāo)和名字
layout.addView(img_item);
layout.addView(tv_item);
return layout;
}
}
這里是用PopupWindow實(shí)現(xiàn),當(dāng)然也可以用AlertDialog或者其他自定義對話框等等,也可以改寫Menu,還可以用Tab實(shí)現(xiàn)。實(shí)現(xiàn)的方法很多,但原理是相同的,例如用兩個(gè)GridView,一個(gè)作為菜單欄,一個(gè)作為菜單項(xiàng)視圖。
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- Android編程實(shí)現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android實(shí)現(xiàn)底部彈出PopupWindow背景逐漸變暗效果
- Android入門之PopupWindow用法實(shí)例解析
- Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法
- android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單
- Android PopupWindow實(shí)現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android簡單使用PopupWindow的方法
相關(guān)文章
Android requestFocus詳解及實(shí)例
這篇文章主要介紹了Android requestFocus詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07Kotlin開發(fā)的一些實(shí)用小技巧總結(jié)
Kotlin 是一個(gè)基于 JVM 的新編程語言,用 JetBrains 的話來說是「更現(xiàn)代化、更強(qiáng)大,所以下面這篇文章主要給大家總結(jié)介紹了關(guān)于Kotlin的一些開發(fā)實(shí)用小技巧,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10Android 自動(dòng)判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用
本篇文章小編為大家介紹,在Android中 自動(dòng)判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用。需要的朋友參考下2013-04-04Android常用正則表達(dá)式驗(yàn)證工具類(實(shí)例代碼)
正則表達(dá)式,相信接觸過編程的人都知道,但是大部分人應(yīng)該是每次用的時(shí)候現(xiàn)找,但對其語法應(yīng)該只是一知半解 。下面小編給大家分享Android常用正則表達(dá)式驗(yàn)證工具類,感興趣的朋友一起看看吧2017-10-10Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
Json是一種類似于XML的通用數(shù)據(jù)交換格式,具有比XML更高的傳輸效率;本文將介紹兩種方法解析JSON數(shù)據(jù),需要的朋友可以參考下2012-12-12SurfaceView開發(fā)[捉小豬]手機(jī)游戲 (二)
這篇文章主要介紹了用SurfaceView開發(fā)[捉小豬]手機(jī)游戲 (二)本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08Android?TextView跑馬燈實(shí)現(xiàn)原理及方法實(shí)例
字的跑馬燈效果在移動(dòng)端開發(fā)中是一個(gè)比較常見的需求場景,下面這篇文章主要給大家介紹了關(guān)于Android?TextView跑馬燈實(shí)現(xiàn)原理及方法的相關(guān)資料,需要的朋友可以參考下2022-05-05Android?完整購物商城界面的實(shí)現(xiàn)案例
這篇文章為大家?guī)硪粋€(gè)Android?完整購物商城的界面具體的實(shí)現(xiàn),案例中含有商品列表的顯示,為商城最重要的功能之一,感興趣的朋友來看看吧2022-03-03ExpandableListView實(shí)現(xiàn)簡單二級列表
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)簡單二級列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11