popupWindow.showAtLocation();
时间: 2024-03-27 13:39:38 浏览: 22
`showAtLocation()`是另一种设置PopupWindow位置的方法,与`showAsDropDown()`不同,它可以在屏幕的任意位置弹出PopupWindow。以下是`showAtLocation()`的使用方法:
```java
popupWindow.showAtLocation(parent, gravity, x, y);
```
其中,`parent`是PopupWindow的父布局,`gravity`是PopupWindow相对于父布局的位置,`x`和`y`是PopupWindow的偏移量。
`gravity`参数可以设置为以下值:
- `Gravity.TOP`
- `Gravity.BOTTOM`
- `Gravity.LEFT`
- `Gravity.RIGHT`
- `Gravity.CENTER`
- `Gravity.CENTER_HORIZONTAL`
- `Gravity.CENTER_VERTICAL`
- 等等
例如,如果想让PopupWindow在屏幕的底部中间位置弹出,可以这样设置:
```java
popupWindow.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
```
这样就可以弹出一个位于屏幕底部中间的PopupWindow了。
相关问题
View popupView = getLayoutInflater().inflate(R.layout.showlist, null); // 获取弹窗布局中的控件 TextView titleView = popupView.findViewById(R.id.title); TextView contentView = popupView.findViewById(R.id.content); // 设置弹窗标题和内容 titleView.setText("name"); contentView.setText(stringList.toString()); // 创建弹窗 PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); // 设置弹窗的背景和动画 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.PopupAnimation); // 显示弹窗 popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);在弹窗中显示List<String>
可以将List<String>转换成字符串,然后将其设置为弹窗中的内容。代码如下:
```
List<String> stringList = new ArrayList<>();
// 将List<String>转换成字符串
String contentStr = TextUtils.join(", ", stringList);
// 获取弹窗布局中的控件
View popupView = getLayoutInflater().inflate(R.layout.showlist, null);
TextView titleView = popupView.findViewById(R.id.title);
TextView contentView = popupView.findViewById(R.id.content);
// 设置弹窗标题和内容
titleView.setText("name");
contentView.setText(contentStr);
// 创建弹窗
PopupWindow popupWindow = new PopupWindow(popupView,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// 设置弹窗的背景和动画
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setAnimationStyle(R.style.PopupAnimation);
// 显示弹窗
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
```
public void onClink(View view, int position) { //closeLoad(); // 获取列内容 // String str = TextUtils.join(", ", stringList); // 创建弹窗布局 View popupView = getLayoutInflater().inflate(R.layout.showlist, null); // 获取弹窗布局中的控件 TextView titleView = popupView.findViewById(R.id.title); TextView contentView = popupView.findViewById(R.id.content); // 设置弹窗标题和内容 titleView.setText("name"); for (ExcelImportViewModel inventory : iv.inventoryList) { stringList.add(inventory.name + inventory.barCode) ; } contentView.setText(stringList.toString()); // 创建弹窗 PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT); // 设置弹窗的背景和动画 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.PopupAnimation); // 显示弹窗 popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0); }点击空白关闭弹窗
如果你想要在点击弹窗外部的空白区域时关闭弹窗,可以通过设置弹窗的属性来实现。具体来说,你可以设置弹窗的`setOutsideTouchable`和`setFocusable`属性,然后在弹窗外部点击事件的处理方法中关闭弹窗。以下是修改后的代码示例:
```java
public void onClink(View view, int position) {
// 创建弹窗布局
View popupView = getLayoutInflater().inflate(R.layout.showlist, null);
// 获取弹窗布局中的控件
TextView titleView = popupView.findViewById(R.id.title);
TextView contentView = popupView.findViewById(R.id.content);
// 设置弹窗标题和内容
titleView.setText("name");
for (ExcelImportViewModel inventory : iv.inventoryList) {
stringList.add(inventory.name + inventory.barCode);
}
contentView.setText(stringList.toString());
// 创建弹窗
PopupWindow popupWindow = new PopupWindow(popupView,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// 设置弹窗的背景和动画
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setAnimationStyle(R.style.PopupAnimation);
// 设置弹窗的属性
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
// 显示弹窗
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// 处理弹窗外部点击事件,关闭弹窗
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// 执行弹窗关闭时的操作
// 例如,清空列表内容
stringList.clear();
}
});
}
```
上述代码中,`popupWindow.setOutsideTouchable(true)`和`popupWindow.setFocusable(true)`分别设置了弹窗的可点击和可聚焦属性,这样在弹窗外部点击事件的处理方法中才能关闭弹窗。`popupWindow.setOnDismissListener`设置了弹窗关闭时的监听器,当弹窗关闭时可以在该监听器中执行一些操作,例如清空列表内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)