DialogFragment's width takes the whole screen unlike AlertDialog

风格不统一 提交于 2019-12-11 17:13:27

问题


I am trying to inflate a CustomView in my dialogFragment class, it works fine but the width takes the whole screen (90%) but if I inflate the same layout in AlertDialog, it just Works fine i.e width seems proper & good.

I also tried setting the DialogFragment's width via onStart() but it wasn't the desired result...

This is what I want (CustomView in AlertDialog)

but here's the Result via DialogFragment - more width is taken as compared to AlertDialog!

Layout File -

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lz_gridChooserParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.lazygeniouz.colorpicker.FillGridView
            android:id="@+id/lz_grid"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnWidth="@dimen/colorchooser_circlesize"
            android:clipToPadding="false"
            android:gravity="center"
            android:horizontalSpacing="8dp"
            android:numColumns="auto_fit"
            android:orientation="vertical"
            android:stretchMode="columnWidth"
            android:verticalSpacing="8dp"
            android:padding="16dp"/>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left|bottom"
                android:text="Custom"
                android:id="@+id/lz_gotoCustom"
                android:background="?attr/selectableItemBackground"
                android:layout_margin="10dp"
                android:typeface="serif"
                android:textStyle="bold"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|bottom"
                android:text="@string/back"
                android:id="@+id/lz_back"
                android:background="?attr/selectableItemBackground"
                android:layout_margin="10dp"
                android:typeface="serif"
                android:textStyle="bold"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:text="@string/done"
                android:id="@+id/lz_done"
                android:background="?attr/selectableItemBackground"
                android:layout_margin="10dp"
                android:typeface="serif"
                android:textStyle="bold"/>

        </FrameLayout>

    </LinearLayout>

</ScrollView>

回答1:


If you are planning to run this application on a fixed size devices than it is ok, but if your application will run on different size devices (phones of different screen sizes or tablets) than consider applying match_parent on your fragment container, and apply layout padding from parent container with the proportion of empty space you need to surround your fragment.

This will let you control the size of your fragment and at the same time will allow smooth resizing in case of different screen sizes.

For example, if you inflate your activity layout from XML file that contains linear layout root element and a fragment child element, the padding is applied on linear layout element and the match_parent will be for the fragment element. in such case you choose a relevant padding figures that will let you achieve your targets.

I hope this provide some useful implementation ideas to you.




回答2:


try what worked by me:

my class is extends AlertDialog instead of DialogFragment,

and inside the class I override show function as follows:

 @Override
    public void show() {
        super.show();
        getWindow().setLayout(500, 300);

and then i open it like this:

MyDialog myDialog = new MyDialog(getActivity());
                myDialog.show();

hope it helps!




回答3:


this is what I did, I just set the DialogFragment's width to 520 & it gave me exactly what I wanted :)



来源:https://stackoverflow.com/questions/47736791/dialogfragments-width-takes-the-whole-screen-unlike-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!