Android get full width for custom Dialog

后端 未结 16 625
慢半拍i
慢半拍i 2020-12-02 15:19

in my application my created custom dialog dont have full height and i can not change and customize that.for example see this screen shot:

相关标签:
16条回答
  • 2020-12-02 15:55
    @Override public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow()
                    .setLayout((int) (getScreenWidth(getActivity()) * .9), (int)(getScreenHeight(getActivity()) * .6) );
        }
    }
    
    public static int getScreenWidth(Activity activity) {
        Point size = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(size);
        return size.x;
    }
    
    public static int getScreenHeight(Activity activity) {
        Point size = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(size);
        return size.y;
    }
    

    example of 90% width and 60% height

    0 讨论(0)
  • 2020-12-02 15:56

    In my case width of custom dialog shrinks in size(width) as the content inside dialog becomes smaller in width even though the the width property was set

    android:layout_width="match_parent"
    

    I just fixed that width to screen size and now its working according to my requirement

    android:layout_width="320dp"
    
    0 讨论(0)
  • 2020-12-02 15:57
    Dialog dialog = new Dialog(BASE_CONTEXT, R.style.Theme_Dialog);  
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);  
    dialog.setContentView(R.layout.your_layout);  
    dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    
    0 讨论(0)
  • 2020-12-02 15:59

    Instead of using custom dialog , use activity class for this. In manifest file specify the style attribute as below

    android:theme="@style/AppThemeDialog"

    And add AppThemeDialog in style as below

    <style name="AppThemeDialog" parent="Theme.AppCompat">

    <item name="colorBackgroundFloating">#ff424242</item> <item name="listPreferredItemPaddingLeft">0dp</item> <item name="android:windowIsFloating">true</item> <item name="listPreferredItemPaddingRight">0dp</item> </style>

    0 讨论(0)
提交回复
热议问题