How to set the width of a DialogFragment in percentage?

后端 未结 2 2163
[愿得一人]
[愿得一人] 2021-02-15 01:19

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<

相关标签:
2条回答
  • 2021-02-15 01:40

    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)
  • 2021-02-15 01:55

    You can achieve this by setting a style to your dialogfragment on it's onCreate method:

    setStyle(DialogFragment.STYLE_NORMAL,R.style.yourStyle);
    

    and in your styles.xml

       <style name="yourStyle" parent="android:Theme.Dialog">
            <item name="android:windowBackground">@drawable/some_background</item>
            <item name="android:windowAnimationStyle">@null</item>
            <item name="android:backgroundDimEnabled">false</item>
            <item name="android:windowMinWidthMajor">75%</item>
            <item name="android:windowMinWidthMinor">75%</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    
    0 讨论(0)
提交回复
热议问题