How to set the width of a DialogFragment in percentage?

后端 未结 1 1193
忘了有多久
忘了有多久 2021-02-15 01:13

I have a DialogFragment with an xml layout and I need it to be, let\'s say 75% as wide as the screen. When I look for answers I always find the common weight<

相关标签:
1条回答
  • 2021-02-15 01:55

    I don't think it is possible to achieve this without using code.

    Anyway I'm using this code inside my custom DialogFragment class. Hope it helps.

    public void onResume() {
        super.onResume();
    
        Window window = getDialog().getWindow();
        Point size = new Point();
    
        Display display = window.getWindowManager().getDefaultDisplay();
        display.getSize(size);
    
        int width = size.x;
    
        window.setLayout((int) (width * 0.75), WindowManager.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);
    }
    
    0 讨论(0)
提交回复
热议问题