• 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;
                }
  • 相关阅读:
    chrome下不支持select里面的option单击事件!
    实现自适应宽度圆角按钮的方法
    jQuery分析(取DOM元素)
    字符串转换成JSON
    限制数量不可为0,且不大于1000
    计算用字符串表示的整数四则运算的值
    计算用字符串表示的个位数四则运算的值(栈)
    螺旋数组
    Joseph问题(循环链表)
    C/C++计算阶乘n!末尾所含0的个数
  • 原文地址:https://www.cnblogs.com/java-g/p/4149677.html
Copyright © 2020-2023  润新知