如何居中弹出窗口?

2024-04-14移动开发问题
7

本文介绍了如何居中弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:

    private void showPopup() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);

    final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);

    Button btnAgree = (Button) layout.findViewById(R.id.ok);
    btnAgree.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            pwindo.dismiss();
        }
    });

    pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
    }
}

I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?

解决方案

you can use setHorizontalOffset:

ListPopupWindow popupWindow = new ListPopupWindow(this);

// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);

这篇关于如何居中弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

正确的 cocos2d 场景重启?
Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)...
2024-08-12 移动开发问题
7

游戏中心 facebook 喜欢
Game center facebook like(游戏中心 facebook 喜欢)...
2024-08-12 移动开发问题
3

Cocos2d - 对 CCSprite 应用 GLImageProcessing 效果
Cocos2d - apply GLImageProcessing effect to CCSprite(Cocos2d - 对 CCSprite 应用 GLImageProcessing 效果)...
2024-08-12 移动开发问题
10

[CALayer 保留]:消息发送到解除分配的实例?
[CALayer retain]: message sent to deallocated instance?([CALayer 保留]:消息发送到解除分配的实例?)...
2024-08-12 移动开发问题
9

Cocos2d-iphone 支持 iPhone 4 高分辨率
support of Cocos2d-iphone for iPhone 4 high res(Cocos2d-iphone 支持 iPhone 4 高分辨率)...
2024-08-12 移动开发问题
10

收到消息“警告:在正常情况下,_fillInQueueWithExtraSpace:.."和 MPMovieP
Got the message quot;WARNING: under normal conditions, _fillInQueueWithExtraSpace:..quot; and MPMoviePlayer rotation not work in iPad IOS 5.1(收到消息“警告:在正常情况下,_fillInQueueWithExtraSpace:..和 MPMoviePlayer 旋转在 iPad IOS 5.1 中...
2024-08-12 移动开发问题
6