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

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值