12

我刚刚使用了第一个 Dialog 使用 DialogFragment 创建的。一切都很好,除了我无法让对话框包装它的布局。我的布局将所有元素的高度设为wrap_content.

MyFragmentDialog我什至找不到暗示它可以用来设置高度的方法FragmentDialog。我错过了什么?如何使 DialogFragment 适合它的内容?

的方法DialogFramentonCreateView

    @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

4 回答 4

29

LinearLayout尽管在其上设置了高度,但事实证明这是一个问题,DialogFragment因为它似乎占用了比我想要的更多的空间。我将布局切换为RelativeLayout内容Dialog似乎会调整大小以适应内容。

于 2012-01-22T18:08:48.610 回答
6

您可以在 dialogFragment 的 onResume 中添加布局的大小,使其适用于所有类型的布局:

  @Override
    public void onResume()
    {
        super.onResume();
        Window window = getDialog().getWindow();
        window.setLayout(300, 300);
        window.setGravity(Gravity.CENTER);
}
于 2015-02-06T14:59:19.270 回答
4

我跳进了对话框 api,所以我当然不确定,但你可以尝试在对话框上调用 getWindow,然后调用 setLayout(width, height)

getDialog().getWindow().setLayout(300,300);
于 2012-01-22T01:58:06.383 回答
0

我没有看到您的 LinearLayout 标签的顶部,但我已经看到了只需要

android:orientation="vertical"

XML 属性。它可能会导致头痛,但幸运的是很容易解决。

于 2012-03-27T16:28:01.823 回答