Android二十二天PopupWindow

Android中有两种弹出框 ,一种是AlertDialog 一种是PopupWindow。

区别:

AlertDialog的位置固定,而PopupWindow的位置可以随意
 •AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
 •PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,

 

使用:

1,通过布局加载器,加载一个布局:getLayoutInflater().inflate(布局资源id,null);

2,创建一个popupwindow对象,new popwindow(要在popupwindow显示的是视图,宽度,高度);

3,设置背景:setBackgroundDrawable(background);建议直接在popupwindow展示的布局中设置背景

4,显示出来

        1,.showAsDropDown(v);显示在v组件的正下方

        2,.showAsDropDown(v, xoff, yoff)显示在相对v组件的正下方x轴偏移xoff,y轴偏移yoff

        3.showAsDropDown(v, xoff, yoff,gravity)

      4,.showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

5,设置popupwindow可点击即点击事件

6,设置点击popupwindow以外是否可隐藏---(.setOutsideTouchable(true);)

代码:

1.创建布局文件 作为弹出框PopupWindow展示的视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="添加"
        android:textColor="@android:color/white" />

    <TextView

        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="添加"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="添加"
        android:textColor="@android:color/white" />

</LinearLayout>

2.在MainActivity中:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //弹出框对象
    private PopupWindow popupWindow;
    //用于展示弹出框位置的文本
    private TextView showTv;
    //用于展示弹出框位置的 父布局
    private LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showTv = (TextView) findViewById(R.id.show_tv);
        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
    }

    public void onButtonClick(View view) {
        //实例化xml成为一个View 视图对象 PopupWindow展示的视图对象
        View popView = LayoutInflater.from(this).inflate(R.layout.pop_layout, null);
        //实例化
        TextView tv1 = (TextView) popView.findViewById(R.id.tv1);
        TextView tv2 = (TextView) popView.findViewById(R.id.tv2);
        TextView tv3 = (TextView) popView.findViewById(R.id.tv3);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);

        /**
         * PopupWindow的构造方法
         *参数一:PopupWindow弹出后展示的视图
         *参数二:展示视图的宽度
         *参数三:展示视图的高度
         * 宽高的表达方式:
         * 1.固定值
         * 2.LayoutParams.WRAP_CONTENT
         * 3.LayoutParams.MATCH_PARENT
         *
         * LayoutParams  Java代码中提供布局视图属性参数的类
         * LayoutParams在哪个包下,那么就具备那个布局视图的属性
         */
        popupWindow = new PopupWindow(popView, RelativeLayout.LayoutParams.WRAP_CONTENT, 600);
        //设置 点击空白区域 弹出框是否消失
        popupWindow.setOutsideTouchable(true);
        //展示方法:
        //1.表示 在哪个视图的下方展示出来
        //popupWindow.showAsDropDown(showTv);
        /**
         * 2. 表示在那个视图下方展示出来
         *  参数2x轴偏移量
         *  参数3y轴的偏移量
         */
        //popupWindow.showAsDropDown(showTv, 100, 0);
        /**
         * 3.
         *该方法要求API19以上 需要添加这个判断 该判断可以通过快捷键生成
         * 1.先写出该方法然后在该方法上 alt+回车
         * 该方法第四个参数表示 相对于父布局的位置
         */
        //3.if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        //    popupWindow.showAsDropDown(showTv, 50, 50, Gravity.BOTTOM);
        //}

        /**
         * 4.
         * 按照父布局的内部位置来展示PopupWindow
         * 参数1:父布局的视图
         * 参数2:内部对其方式   其展示位置
         * 参数3x轴偏移量
         * 参数4Y轴偏移量
         *
         */
        popupWindow.showAtLocation(linearLayout, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv1:
                Toast.makeText(this, "按钮1", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv2:
                Toast.makeText(this, "按钮2", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv3:
                Toast.makeText(this, "按钮3", Toast.LENGTH_SHORT).show();
                break;
        }
        //  PopupWindow消失
        popupWindow.dismiss();
    }


    /**
     * PopupWindow显示的时候 按下物理返回键 优先关闭PopupWindow
     * PopupWindow没有显示的时候无须处理
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断一下按下的按钮是 物理返回键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //判断一下PopupWindow是否是显示状态
            if (popupWindow.isShowing()) {
                //如果是显示状态 消失
                popupWindow.dismiss();
                //返回true表示消费了此次事件
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值