PopupWindow弹出窗口

本文详细介绍了如何在Android中使用PopupWindow创建一个弹出窗口,并通过指定坐标来精确控制其位置。示例代码包括触发按钮、获取屏幕坐标、初始化弹出窗口布局以及配置显示位置的完整流程。

Android中的弹出窗口有很多,可以是PopupMenu或者AlertDialog等。但是相对于其他方式,只有PopupWindow弹出窗口是最灵活的。它可以出现在窗口的任意位置。


例子:

  • iv_head_right_activity是触发的按钮

case R.id.iv_head_right_activity:
int[] location = new int[2];
//得到下x,y坐标,并且保存在数组location[]中
// location[0] = x, location[1] = y.
view.getLocationOnScreen(location);
//初始化化x,y坐标
point = new Point();
point.x = location[0];
point.y = location[1];
//弹出PopupWindow
showPublishPopup(MainActivity.this, point);
break;

  • 布局文件如下:R.layout.popupwindow_activity
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:background="@android:color/transparent"
    android:id="@+id/ll_popupwindow"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/ll_recode"
        android:gravity="center"
        android:layout_width="180dp"
        android:layout_marginRight="10dp"
        android:background="#21292b"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:padding="15dp"
            android:text="@string/activity_activity_recode"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginRight="10dp"
        android:background="@android:color/background_dark"/>
    <LinearLayout
        android:id="@+id/ll_pubish"
        android:gravity="center"
        android:layout_width="180dp"
        android:background="#21292b"
        android:layout_marginRight="10dp"
        android:layout_height="wrap_content">
        <TextView
            android:textColor="@android:color/white"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="15dp"
            android:text="@string/activity_pubish"/>
    </LinearLayout>

</LinearLayout>
  • showPublishPopup是弹出窗口的方法
    private void showPublishPopup(final Activity context, Point p) {

            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popupLayout = layoutInflater.inflate(R.layout.popupwindow_activity, null);
            // 创建 PopupWindow
            popupWindow = new PopupWindow(context);
                  popupWindow.setContentView(popupLayout);
            popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
            popupWindow.setFocusable(true);
           //设置相对于触发弹出窗口的按钮的左边OFFSET_X和下面OFFSET_Y的偏移量
            int OFFSET_X = -150;
            int OFFSET_Y = 105;
            //去掉默认的半透明背景
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            // 显示在某个位置
            popupWindow.showAtLocation(popupLayout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
            llPubish=(LinearLayout)popupLayout.findViewById(R.id.ll_pubish);
            llPubish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewInject.toast("发布");
                }
            });
            llRecode=(LinearLayout)popupLayout.findViewById(R.id.ll_recode);
            llRecode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewInject.toast("记录");
                }
            });
    }
  • 结果如下图

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值