PopupWindow --- 弹出底部窗体

第一步 : 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
    android:id="@+id/menu_close"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/menu_bg"    //背景图片    9
        android:gravity="center"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/menu_close_btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/menu_btn_bg"   // 选择器
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/btn_close" />   // 关闭的图标
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="退出"
                android:textColor="#eee" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

第二步 点击 事件 调出 PopupWindow 的方法

    if (!menu_display) {    //如果没有显示
                // 获取LayoutInflater实例
                inflater = (LayoutInflater) this
                        .getSystemService(LAYOUT_INFLATER_SERVICE);
                // 这里的main布局是在inflate中加入的哦,以前都是直接this.setContentView()的吧?呵呵
                // 该方法返回的是一个View的对象,是布局中的根
                layout = inflater.inflate(R.layout.main_menu, null);
                menuWindow = new PopupWindow(layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); // menuWindow.showAsDropDown(layout); //设置弹出效果
                // menuWindow.showAsDropDown(null, 0, layout.getHeight());
                menuWindow.showAtLocation(this.findViewById(R.id.mainweixin),  // 整个布局的下面 
                        Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
                // 如何获取我们main中的控件呢?也很简单
                mClose = (LinearLayout) layout.findViewById(R.id.menu_close);
                mCloseBtn = (LinearLayout) layout
                        .findViewById(R.id.menu_close_btn);

                // 下面对每一个Layout进行单击事件的注册吧。。。
                // 比如单击某个MenuItem的时候,他的背景色改变
                // 事先准备好一些背景图片或者颜色
                mCloseBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        // Toast.makeText(Main.this, "退出",
                        // Toast.LENGTH_LONG).show();
                        Intent intent = new Intent();
                        intent.setClass(MainWeixin.this, Exit.class);
                        startActivity(intent);
                        menuWindow.dismiss(); // 响应点击事件之后关闭Menu
                    }
                });
                menu_display = true;
            } else {  //显示了
                // 如果当前已经为显示状态,则隐藏起来
                menuWindow.dismiss();
                menu_display = false;
            }

 

posted @ 2014-12-07 18:30  每天学习一点点...  阅读(377)  评论(0编辑  收藏  举报