【问题标题】:DialogFragment set height of DialogDialogFragment 设置Dialog的高度
【发布时间】:2012-01-22 00:59:52
【问题描述】:

我刚刚使用了由第一个 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>

谢谢,

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案1】:

    尽管设置了高度,LinearLayout 还是存在问题,但在 DialogFragment 中,它似乎占用的空间比我想要的要多。我将布局切换为RelativeLayout,内容Dialog 似乎调整大小以适应内容。

    【讨论】:

    • 你能发布解决方案吗?您是否为相对布局或 wrap_content 设置了高度?
    • 宽度为match_parent,高度为wrap_content。如果您还需要我发布解决方案,请告诉我。
    • 感谢 Ali.i 解决了问题。我用宽度和高度为 match_parent 的相对布局包装了现有布局。当然,如果需要,您可以发布对其他人有用的答案。
    • 如果你使用相对布局你不能使用对齐父底部,对齐父顶部等。所有项目必须高于或低于其他项目。一旦您与父顶部或底部对齐,它就会增长以填满屏幕
    • @Kevin 是正确的,但是对于我的用例来说这并不是真正的问题。
    【解决方案2】:

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

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

    【讨论】:

    • 对于这个愚蠢的问题,我在 SO 上找到了 100 多个解决方案,这是一个有效的解决方案,让我多一天保持清醒。
    • @Gyome 请参考这个问题-stackoverflow.com/questions/45494644/…
    【解决方案3】:

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

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

    【讨论】:

    • 谢谢 Bram,但事实证明这是 LinearLayout 的问题,我将其切换为 RelativeLayout,内容 Dialog 似乎调整大小以适应内容。
    【解决方案4】:

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

    的情况
    android:orientation="vertical"
    

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

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      相关资源
      最近更新 更多