开发者

How to set MaxWidth of an Android Dialog?

开发者 https://www.devze.com 2023-03-08 10:42 出处:网络
I have a dialog with a custom style that\'s using a layout that I defined in an XML file. I\'d like this dialog to fill the width of the screen in portrait mode, but only about 400dip in lands开发者_J

I have a dialog with a custom style that's using a layout that I defined in an XML file. I'd like this dialog to fill the width of the screen in portrait mode, but only about 400dip in lands开发者_JS百科cape mode. MaxWidth seems to be the perfect way to accomplish that, but I can't figure out how to assign a MaxWidth to the dialog style or the XML layout.


Assuming you use next layout file called layout/dialog_core.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" >

    //All your dialog views go here.... 

</LinearLayout>

You can then create two more files: layout/dialog.xml and layout-land/dialog.xml.

The layout/dialog.xml file will not limit the width:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" >

    <include layout="@layout.dialog_core.xml" />

</LinearLayout>

While your layout-land/dialog.xml should have width limitation:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"
    android:maxWidth="400dp" >

    <include layout="@layout.dialog_core.xml" />

</LinearLayout>

And of course, you need to use R.layout.dialog layout in your code now.

P.S. I used LinearLayout for example purposes only. Any layout view will do.


You can find an actual solution here: setting a max width for my dialog

The answer is for an activity using a dialog theme, but considering it's based on a Window object, it should be too complicated to adapt it for a Dialog.


I achieved this for a BottomSheetDialog by changing the dialog's window's layout after the dialog was shown:

private int maxWidthDp = 600;

public void showDialog(Context context) {
    BottomSheetDialog dialog = new BottomSheetDialog(context);
    // set up the dialog...
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            Configuration configuration = context.getResources().getConfiguration();
            if (configuration.screenWidthDp > maxWidthDp) {
                // ensure maximum dialog size
                dialog.getWindow().setLayout(dpToPixels(maxWidthDp), -1);
            }
        }
    });
    dialog.show();
}

dpToPixels is a helper function that converts dp values to pixel values, e.g. like here https://stackoverflow.com/a/8399445/1396068


Have you tried to set android:minWidth and android:minHeight in your custom xml

0

精彩评论

暂无评论...
验证码 换一张
取 消