与AlertDialog不同,DialogFragment的宽度占据了整个屏幕 [英] DialogFragment's width takes the whole screen unlike AlertDialog

查看:123
本文介绍了与AlertDialog不同,DialogFragment的宽度占据了整个屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的dialogFragment类中添加一个CustomView,它可以正常工作,但是宽度占据了整个屏幕的90%,但是如果我在AlertDialog中对相同的布局进行膨胀,它就可以正常工作,即宽度似乎是正确的&好.

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.

我还尝试通过onStart()设置DialogFragment的宽度,但这不是理想的结果...

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

这就是我想要的(AlertDialog中的CustomView)

This is what I want (CustomView in AlertDialog)

但是这是通过DialogFragment获得的结果- 与AlertDialog相比,宽度更大!

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

布局文件-

    <?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>

推荐答案

如果您打算在固定大小的设备上运行此应用程序,则可以,但是如果您的应用程序将在不同大小的设备(不同屏幕的电话)上运行大小或平板电脑),而不是考虑在您的片段容器上应用match_parent,并从父容器中以需要包围片段的空白空间的比例来应用布局填充.

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.

例如,如果您从包含线性布局根元素和片段子元素的XML文件中扩展活动布局,则将填充应用于线性布局元素,并且match_parent将用于片段元素.在这种情况下,您可以选择一个相关的填充数字,以实现您的目标.

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.

这篇关于与AlertDialog不同,DialogFragment的宽度占据了整个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆