原创,如转载请标明链接:http://blog.csdn.net/q610098308/article/details/50333387
很多App,都有二级菜单出现,但android 本身实现的菜单又比较难看;前些天我自己思想了一个,供大家学习,为方便学习,这里我只用最简单的字符串菜单,如果大家想用更复杂或好看的,可以自定义listview 的子项即可。
一.首先看一下实现思路:
1. 使用上方按钮来弹出一级菜单,如果在弹出状态,再点击隐藏;
2. 点击一级菜单子项,弹出二级菜单,二级菜单里面也是一个listview 控件;
3. 点击二级菜单点击菜单的内容;
二 .实现
就是这么简单,下面来看一下具体实现:
1.在AndroidManifest.xml 设置主题为NoTitleBar
android:theme="@style/Theme.AppCompat.NoActionBar">2.在布局文件中添加自定义titlebar,并添加 listview 控件在title 下方,见布局文件;
- <?xml version="1.0" encoding="utf-8"?>
- <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"
- tools:context="com.shrg.jackli.morelevelmenu.MainActivity">
-
- <LinearLayout
- android:id="@+id/titlebar"
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:orientation="horizontal"
- android:background="@android:color/black"
- android:gravity="center_vertical">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="showListMenu"
- android:src="@mipmap/ic_menu"
- />
- </LinearLayout>
- <ListView
- android:id="@+id/listView"
- android:background="@android:color/holo_blue_dark"
- android:layout_below="@id/titlebar"
- android:layout_width="100dp"
- android:layout_height="wrap_content"
- ></ListView>
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="New Text"
- android:textSize="25sp"
- android:id="@+id/textView"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
-
- </RelativeLayout>
3.程序中将listview 控件关联起来 ,将listview 填充数据,监听listview 每一项
- mListView =(ListView)findViewById(R.id.listView);
- mListView.setVisibility(View.GONE);
- mListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,province));
-
- mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- pop_listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
- android.R.layout.simple_list_item_1,pandc[position]));
- firstMenuIndex = position;
- popupWindow.showAsDropDown(mListView,view.getMeasuredWidth(),-mListView.getMeasuredHeight());
- }
- });
4.初始化popuwindow,监听popuwindow中listview 中子项,
监听到点击二级菜单后隐藏一级和二级菜单
- private void initPopupWindow() {
- pop_listView = (ListView)LayoutInflater.from(this).inflate(R.layout.popupwindow,null);
- pop_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- showText.setText(pandc[firstMenuIndex][position]);
- popupWindow.dismiss();
- mListView.setVisibility(View.GONE);
- }
- });
-
- popupWindow = new PopupWindow(pop_listView, 150,
- ViewGroup.LayoutParams.WRAP_CONTENT);
-
- popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
- popupWindow.setFocusable(true);
- }
效果图: