用popupwindow做一个spinner的坑记录

1. 定位的问题

ppw提供了两种show的方式

showAsDropDown

showAtLocation

调用这两个方法都需要传入一个用于定位的view,showAsDropDown叫anchor,showAtLocation叫parent

showAsDropDown是在anchor的下方将ppw显示出来,没怎么用这个,暂且空下.

showAtLocation是在所在的window里面定位,parent只是为了提供window的一些数据

parent: a parent view to get the android.view.View.getWindowToken() token from

所以不管parent在什么位置,有多大,在Gravity设定好之后,ppw出现的位置都是给予window的四个角,然后再根据偏移的参数具体定位。

再说偏移参数x,y,并不是正的就向右下,负的就向左向上,而是基于四个角的位置向内

比如y>0, 如果设置Gravity.TOP,就是向下偏移,Gravity.BOTTOM就是向上偏移



2. ppw宽度的问题

我的ppw中包含一个listview,不管怎么设置宽度为自适应,show出来的时候宽度都是matchparent的。

有人说listview在getview之前,不知道宽度是多少,所以就设为最大宽度了,我也没有去细究原因。

参考了两个地方:

http://blog.csdn.net/zgyulongfei/article/details/7973063

http://stackoverflow.com/questions/4085093/how-to-set-the-width-of-the-listview-in-a-popupwindow-on-android/12384748#12384748

都没解决问题,无奈只能使用最后的杀手锏:设置一个固定的宽度

int width = mContext.getResources().getDimensionPixelSize(
				R.dimen.spinner_item_width);
		this.setWidth(width);
在构造函数里面设定一个固定的宽度,奏效,好无奈的感觉,不过listview里面的item的宽度也是这个,所以还好



3. 当快速点击ppw中listview的item时,item会连同背景一同消失,变透明

stackoverflow上面也有这个问题:

http://stackoverflow.com/questions/8977197/a-button-in-a-popupwindow-goes-transparent-when-clicked-quickly

就是快速的点击会出现,慢慢的点击正常。

仍未解决。


在Android开发中,创建自定义PopupWindow通常用于显示弹出视图,比如菜单、对话框等。以下是创建一个简单自定义PopupWindow的基本步骤: 1. **添加依赖**: 首先需要在build.gradle文件中引入`androidx.appcompat.widget.AppCompat_popupwindow`或`com.google.android.material.popupwindow.MaterialPopUpWindow`,如果你使用的是Material Design风格。 ```gradle dependencies { implementation 'com.google.android.material:material:1.4.0' } ``` 2. **创建布局文件**: 创建一个新的XML布局文件,例如popup_layout.xml,这是你的弹出窗口内容。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 弹出窗口内的组件 --> <TextView android:text="Hello, PopupWindow!" /> </LinearLayout> ``` 3. **创建PopupWindow实例**: 在你的Activity或Fragment中,通过LayoutInflater加载布局,并创建PopupWindow对象。 ```java import androidx.appcompat.widget.PopupWindow; // ... private PopupWindow popupWindow; private View popupLayoutView; public void setUpPopup() { LayoutInflater inflater = getLayoutInflater(); popupLayoutView = inflater.inflate(R.layout.popup_layout, null); // 设置弹出窗口的大小 int width = ViewGroup.LayoutParams.WRAP_CONTENT; int height = ViewGroup.LayoutParams.WRAP_CONTENT; popupWindow = new PopupWindow(popupLayoutView, width, height); // 其他设置,如背景透明、动画等 popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setOutsideTouchable(true); popupWindow.setAnimationStyle(R.style.PopupEnterExitAnimation); } // 显示弹出窗口 public void showPopup() { popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER, 0, 0); } // 关闭弹出窗口 public void dismissPopup() { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值