popupwindow显示在指定控件之上

做一个这样的弹框效果,在底部控件上方弹出
在这里插入图片描述

传入指定控件的高度,popupwindow减去这个高度,然后显示的时候,上移这个高度
parentViewHeight 为指定控件的高度

public class ShoppingCarWindow extends PopupWindow{
。。。。
  //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高

      DisplayMetrics displayMetrics=mContext.getResources().getDisplayMetrics();
        int height = displayMetrics.heightPixels;

     
        this.setHeight(height-parentViewHeight);
}


显示

  ShoppingCarWindow shoppingCarWindow =new ShoppingCarWindow(ServicePackageActivity.this,null,10, binding.rlBottom.getHeight());
            shoppingCarWindow.setOnItemClickListener((position, num) -> {
                binding.tvCarNum.setText(num);
            });


            shoppingCarWindow.showAsDropDown(binding.rlBottom, Gravity.TOP,   0, binding.rlBottom.getHeight());

布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    <View
            android:id="@+id/ll_empty"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#80000000"
            />
<LinearLayout
        android:orientation="vertical"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="15dp">


    <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:textColor="@color/gray_9b9b9b"
            android:text="已选服务"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="15dp" />

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginBottom="5dp"
            android:background="@color/white" />


</LinearLayout>
</LinearLayout>

另外一些其他效果需要计算,比如
移动端 android PopupWindow在控件上方显示

让popupwindow显示在某个控件的上方

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值