在一个按钮正下方显示一个popwindow

本文介绍了一个具体的PopupWindow显示示例,包括使用LayoutInflater加载布局、设置ListView适配器以及PopupWindow的位置和尺寸等关键步骤。

 //这是show一个PopupWindow 

  PopupWindow mPopupWindow;

    public void showPopupWindow() {

LayoutInflater mLayoutInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

View music_popunwindwow = mLayoutInflater.inflate(R.layout.portal_select_layout, null);

ListView portal_select_listView = (ListView) music_popunwindwow.findViewById(R.id.portal_select_listView);

ArrayList androi = new ArrayList();

for (int i = 0; i < 10; i++) {

androi.add(""+i);}

portal_select_listView.setAdapter( new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,androi));

//popwindow的宽度和按钮的宽度相同都是230

mPopupWindow = new PopupWindow(music_popunwindwow,230, LayoutParams.WRAP_CONTENT);

mPopupWindow.setFocusable(true);

mPopupWindow.setOutsideTouchable(true);

mPopupWindow.update();

//位置在R.id.button的正下方

mPopupWindow.showAsDropDown(findViewById(R.id.button), 0,5);

}

在鸿蒙系统中(HarmonyOS),自定义一个从屏幕下方弹出的PopupWindow可以通过以下步骤实现: 1. **创建布局文件**:首先,创建一个布局文件(例如`popup_layout.xml`),用于定义PopupWindow的内容。 ```xml <!-- popup_layout.xml --> <LinearLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:orientation="vertical" ohos:layout_width="match_parent" ohos:layout_height="wrap_content" ohos:background="#FFFFFF" ohos:padding="20vp"> <Text ohos:text="这是一个自定义的PopupWindow" ohos:textSize="20fp" ohos:textColor="#000000" ohos:layout_width="wrap_content" ohos:layout_height="wrap_content" /> <!-- 其他组件 --> </LinearLayout> ``` 2. **创建PopupWindow**:在Java代码中创建PopupWindow,并设置其内容和动画效果。 ```java import ohos.agp.components.PopupWindow; import ohos.agp.components.Component; import ohos.agp.utils.LayoutAlignment; import ohos.agp.window.service.Display; import ohos.agp.window.service.DisplayManager; import ohos.app.Context; public class CustomPopupWindow { private PopupWindow popupWindow; public CustomPopupWindow(Context context) { // 加载布局 Component layout = ComponentContainerProvider.createComponentContainerFromLayout(context, "popup_layout.xml"); // 创建PopupWindow popupWindow = new PopupWindow(layout, ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.WRAP_CONTENT); // 设置动画 popupWindow.setAnimationStyle(ohos.R.style.PopupAnimation); // 设置背景 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); // 设置点击外部关闭PopupWindow popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); } public void show(Component anchor) { // 获取屏幕高度 Display display = DisplayManager.getInstance().getDefaultDisplay(); int screenHeight = display.getHeight(); // 计算PopupWindow的起始位置(屏幕下方) int[] location = new int[2]; anchor.getLocationOnScreen(location); int x = location[0]; int y = location[1] + anchor.getHeight(); // 显示PopupWindow popupWindow.showAtLocation(anchor, LayoutAlignment.BOTTOM, x, screenHeight - y); } public void dismiss() { if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } } } ``` 3. **使用PopupWindow**:在需要显示PopupWindow的地方调用`show`方法。 ```java // 假设在一个按钮的点击事件中使用 button.setClickedListener(new Component.ClickedListener() { @Override public void onClick(Component component) { CustomPopupWindow popupWindow = new CustomPopupWindow(this); popupWindow.show(button); } }); ``` 通过以上步骤,你就可以在鸿蒙系统中自定义一个从屏幕下方弹出的PopupWindow了。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值