Show a PopupWindow centralized

前端 未结 2 1013
孤城傲影
孤城傲影 2020-12-08 06:44

I have some popups on my app, it\'s fullscreen and the following code:

    content.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            La         


        
2条回答
  •  时光说笑
    2020-12-08 07:15

    popupWindow.showAtLocation(anyViewOnlyNeededForWindowToken, Gravity.CENTER, 0, 0);
    

    This will center your view.

    It took me 2 hours of pain to figure this out. I didn't need any black magic maths to handle this.

    The view is only needed for the window token, it has no other impact on the location.

    Gravity tells the layout manager where to start the coordinate system and how to treat those coordinates. I can't find the docs but hacking is showing me that:

    • CENTER uses the middle of the popup to be aligned to the x,y specified. So 0,0 is screen centered
    • BOTTOM uses the bottom of the popup to be aligned to the x,y specified. So 0,0 has the popup bottom aligned with the screen bottom. If you want 10px padding then y=10 (not -10) to move the popup up the screen 10 pixels.

    I wrote this up over here Android PopupWindow.showAtLocation() and effects of Gravity

提交回复
热议问题