DialogFragment设置对话框的高度 [英] DialogFragment set height of Dialog
问题描述
我刚刚使用了第一个使用DialogFragment创建的Dialog.
一切工作正常,但我无法获取对话框来包装其布局.
我的布局中所有元素的高度都为wrap_content
.
I just used created by first Dialog using DialogFragment.
Everything works great except I can't get the Dialog to wrap it's layout.
My layout has the height of all elements to wrap_content
.
在MyFragmentDialog
中,我什至找不到一种暗示可以用来设置FragmentDialog
高度的方法.我想念什么?如何使DialogFragment适合其内容?
In MyFragmentDialog
I can't even find a method that would imply that it can be used to set the height of the FragmentDialog
. What am I missing? How do I make a DialogFragment fit it's content?
DialogFrament
的onCreateView
方法:
The DialogFrament
's onCreateView
method:
@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>
谢谢
推荐答案
尽管LinearLayout
设置了高度,但事实证明这是一个问题,在DialogFragment
中似乎占用了比我想要的空间更多的空间到.我将布局切换为RelativeLayout
内容Dialog
似乎已调整大小以适合内容.
It turns out to be an issue with LinearLayout
despite setting a height on it, in DialogFragment
it seems to be taking more space than I want it to. I switched layout to be a RelativeLayout
the content Dialog
seems to resize to fit the content.
这篇关于DialogFragment设置对话框的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!