DialogFragment设置对话框的高度

12
我刚刚使用了DialogFragment创建了一个对话框。 除了无法使对话框自适应其布局之外,一切都很好。 我的布局中所有元素的高度都设置为wrap_content
MyFragmentDialog中,我甚至找不到可以用来设置FragmentDialog高度的方法。我错过了什么?如何让一个DialogFragment适应其内容? DialogFragmentonCreateView方法:
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Set title for this dialog
    getDialog().setTitle("Backup & Restore");
    getDialog().setCancelable(true);

    View v = inflater.inflate(R.layout.backup_restore, container, false);
    TextView msg = (TextView) v.findViewById(R.id.br_label_message);
    msg.setText("Backups are placed in the Downloads Directory:\n" + BACKUP_PATH.getAbsolutePath());
    // TextView files_label = (TextView) v.findViewById(R.id.label_restore);
    Spinner files = (Spinner) v.findViewById(R.id.br_restore_file);

    if (BACKUP_PATH.exists()) {
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String filename) {
                File sel = new File(dir, filename);
                return filename.contains(FTYPE) || sel.isDirectory();
            }
        };
        mFileList = BACKUP_PATH.list(filter);
    } else {
        mFileList = new String[0];
    }

    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, mFileList);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    files.setAdapter(adapter);
    files.setOnItemSelectedListener(this);

    Button backup = (Button) v.findViewById(R.id.br_backup_btn);
    Button restore = (Button) v.findViewById(R.id.br_restore_btn);
    Button cancel = (Button) v.findViewById(R.id.br_cancel_btn);

    backup.setOnClickListener(this);
    restore.setOnClickListener(this);
    cancel.setOnClickListener(this);

    return v;
}

这是布局:

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

    <TextView
        android:id="@+id/br_label_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:text=""
        android:textSize="14sp" />

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

        <TableRow
            android:id="@+id/br_tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/br_label_restore"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="8dp"
                android:text="Restore file"
                android:textSize="14sp" />

            <Spinner
                android:id="@+id/br_restore_file"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>

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

        <TableRow>

            <Button
                android:id="@+id/br_restore_btn"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="Restore"
                android:textSize="14sp" />

            <Button
                android:id="@+id/br_backup_btn"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:text="Backup"
                android:textSize="14sp" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/br_cancel_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:textSize="14sp" />

</LinearLayout>
谢谢。
4个回答

29

尽管在LinearLayout上设置了高度,在DialogFragment中似乎占用的空间比我想要的多,这证明是一个问题。我将布局切换为RelativeLayout后,Dialog的内容似乎会自适应大小。


你能发布解决方案吗?你设置了相对布局的高度还是使用了wrap_content? - Ads
谢谢Ali。我已经找到一种解决方法了。我用一个宽高都是match_parent的相对布局包装了现有的布局。当然,如果需要,您可以发布答案以供他人参考。 - Ads
1
如果您使用相对布局,就不能使用align parent bottom、align parent top等属性。所有的项目都必须在其他项目的上方或下方。一旦您将其与父项的顶部或底部对齐,它就会自动扩展以填满屏幕。 - Kevin
@Kevin,你说得对,但是对于我的使用情况来说并不是真正的问题。 - Ali
@ali请参考这个问题 - https://stackoverflow.com/questions/45494644/how-to-set-the-height-of-the-dialog-fragment-accurately-in-android - Aman Verma
显示剩余2条评论

6

你可以在dialogFragment的onResume中添加布局的大小,使其适用于各种布局:

  @Override
    public void onResume()
    {
        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(300, 300);
        window.setGravity(Gravity.CENTER);
}

在 Stack Overflow 上我找到了数百种解决这个愚蠢问题的方法,但只有这一个有效并让我保持理智了一天。 - Gyome
@Gyome请参考这个问题 - https://stackoverflow.com/questions/45494644/how-to-set-the-height-of-the-dialog-fragment-accurately-in-android - Aman Verma

4

我尝试着使用Dialog API,但并不确定,你可以尝试在对话框上调用getWindow方法,然后调用setLayout(width, height)来设置宽度和高度。

getDialog().getWindow().setLayout(300,300);

谢谢Bram,但事实证明这是一个与LinearLayout有关的问题。我将其切换为RelativeLayout,内容对话框似乎可以调整大小以适应内容。 - Ali

0

我看不到你的LinearLayout标签的顶部部分,但我曾经遇到过只需要

的情况。

android:orientation="vertical"

XML属性。它可能会让人头疼,但幸运的是很容易修复。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接