簡體   English   中英

與AlertDialog不同,DialogFragment的寬度占據整個屏幕

[英]DialogFragment's width takes the whole screen unlike AlertDialog

我試圖在我的dialogFragment類中添加一個CustomView,它可以正常工作,但是寬度占據了整個屏幕的90%,但是如果我在AlertDialog中添加相同的布局,它就可以正常工作,即寬度似乎正確且良好。

我也嘗試通過onStart()設置DialogFragment的寬度,但這不是理想的結果...

這就是我想要的(AlertDialog中的CustomView) 1個

但是這是通過DialogFragment獲得的結果-與AlertDialog相比,寬度更大! 2]

布局文件-

    <?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並應用布局填充從父容器中提取,需要用一定比例的空白空間來包圍片段。

這樣,您可以控制片段的大小,同時可以在屏幕大小不同的情況下平滑調整大小。

例如,如果您從包含線性布局根元素和fragment子元素的XML文件中擴展活動布局,則將填充應用於線性layout元素,並且match_parent將用於fragment元素。 在這種情況下,您可以選擇相關的填充數字,以實現目標。

我希望這會為您提供一些有用的實施思路。

試試我的工作方式:

我的課是擴展AlertDialog而不是DialogFragment,

在類中,我重寫show函數,如下所示:

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

然后我像這樣打開它:

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

希望能幫助到你!

這就是我所做的,我只是將DialogFragment的寬度設置為520,而這正是我想要的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM