popupwindow 如何实现弹出菜单效果_popupwindow 实现弹出窗口范例

来源:互联网 发布:ip切换软件 编辑:程序博客网 时间:2024/03/29 23:45

popupwindow实现弹出菜单功能非常实用,在有布局中经常出现,给用户体验非常不错,以下是我总结popupwindow实现弹出窗口的经常使用范例:

1.  先看效果:

2. main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="match_parent" android:layout_width="match_parent"    android:background="@drawable/bg_5">    <ListView android:id="@+id/lv_id" android:layout_height="wrap_content"        android:layout_width="match_parent" />    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"        android:orientation="horizontal" android:layout_alignParentBottom="true"        android:background="@drawable/title_weekend_bg">        <Button android:id="@+id/but_menu"            android:layout_width="wrap_content" android:layout_height="wrap_content"            android:text="菜单" android:background="@drawable/button_select"/>        <Button android:id="@+id/but_news"            android:layout_width="wrap_content" android:layout_height="wrap_content"            android:text="新闻" android:background="@drawable/button_select"            android:layout_marginLeft="10dp"/>       </LinearLayout></RelativeLayout>

3. popupmenu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="match_parent" android:layout_width="match_parent"    android:background="@drawable/main" android:orientation="vertical">    <TextView android:id="@+id/open_id" android:layout_width="wrap_content"        android:layout_height="wrap_content" android:text="打开"/>    <TextView android:id="@+id/save_id" android:layout_width="wrap_content"        android:layout_height="wrap_content" android:text="w保存"/>    <TextView android:id="@+id/my_id" android:layout_width="wrap_content"        android:layout_height="wrap_content" android:text="我的留言"/>    <TextView android:id="@+id/about_id" android:layout_width="wrap_content"        android:layout_height="wrap_content" android:text="关于我们"/>    <TextView android:id="@+id/help_id" android:layout_width="wrap_content"        android:layout_height="wrap_content" android:text="帮助"/></LinearLayout>

 4. 核心java代码:按下按钮时显示弹出窗口

menuBut.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                LayoutInflater layoutInflater = getLayoutInflater();                View view = layoutInflater.inflate(R.layout.popupmenu,null);                //PopupWindow弹出的窗口显示的view,第二和第三参数:分别表示此弹出窗口的大小                 m_popupWindow = new PopupWindow(view, 150, 250);                //PopupWindow弹出的窗口显示的位置 ,第一个参数指在那个view上 ,第二和第三参数分别表示距离此view的x和y的距离                m_popupWindow.showAsDropDown(m_btnOk, 0,0);            }        });

 5. 关闭弹出的窗口

public boolean onTouchEvent(MotionEvent event) {        //关闭m_popupWindow窗口        m_popupWindow.dismiss();        return super.onTouchEvent(event);    }
转载自:http://www.ideaex.net/html/Article/2011/07/29/281.html
原创粉丝点击