PopupWindow

作者: 大灰狼zz | 来源:发表于2018-08-04 14:25 被阅读0次

效果图: GIF.gif

在layout中新建布局文件R.layout.ui_popupwindow_two_tv.xml

<?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="200dp"
    android:background="@color/pink"
    android:orientation="vertical">

    <TextView
        android:id="@+id/cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/app_name" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/red" />

    <TextView
        android:id="@+id/confirm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="@string/app_name" />

</LinearLayout>

在activity使用

  1. 初始化PopupWindow
 private void initPopupWindow() {
        // 用于PopupWindow的View
        View contentView = LayoutInflater.from(this).inflate(R.layout.home_popupwindow_wheelpicker, null, false);
        // 创建PopupWindow对象,其中:
        // 第一个参数是用于PopupWindow中的View,第二个参数是PopupWindow的宽度,
        // 第三个参数是PopupWindow的高度,第四个参数指定PopupWindow能否获得焦点
        window = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 设置PopupWindow的背景
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        // 设置PopupWindow是否能响应外部点击事件
        window.setOutsideTouchable(true);
        // 设置PopupWindow是否能响应点击事件
        window.setTouchable(true);
        // 显示PopupWindow,其中:
        // 第一个参数是PopupWindow的锚点,第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
//        window.showAsDropDown(btn1);
        // 或者也可以调用此方法显示PopupWindow,其中:
        // 第一个参数是PopupWindow的父View,第二个参数是PopupWindow相对父View的位置,
        // 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
//        window.showAtLocation(btn1, Gravity.BOTTOM, 0, 0);
        contentView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
        contentView.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                window.dismiss();
            }
        });
    }
  1. 使用方法
       window.showAtLocation(textView, Gravity.BOTTOM, 0, 0);

显示位置

关于Android popupWindow的位置显示 showAtLocation理解
Android中使PopupWindow显示在指定控件的上下左右!

相关文章

网友评论

    本文标题:PopupWindow

    本文链接:https://www.haomeiwen.com/subject/riksvftx.html