PopupWindow

效果图:
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显示在指定控件的上下左右!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,093评论 25 709
  • 在Android中弹出式菜单(以下称弹窗)是使用十分广泛一种菜单呈现的方式,弹窗为用户交互提供了便利。关于弹窗的实...
    OzanShareing阅读 8,320评论 2 10
  • PopupWindow是Android上自定义弹出窗口,使用起来很方便。 PopupWindow的构造函数为 pu...
    OzanShareing阅读 4,155评论 1 2
  • 对待宝宝一定要有耐心,她才11个月,他什么都不懂呀。 昨晚上大概从三点开始,宝宝就醒了,开始就是睁开眼睛到处看,然...
    forbetterfamily阅读 1,801评论 0 1
  • 2018年6月23日,天气晴,星期六 早上的晨练只剩下我们两个人了,她们都有事请假了。貌似人少了我们走的...
    执手踏青_f553阅读 1,135评论 0 1

友情链接更多精彩内容