Диалоговое окно "Диалог" устанавливает высоту диалога
Я только что создал созданный с помощью DialogFragment первый диалог.
Все работает отлично, за исключением того, что я не могу заставить Dialog обернуть его макет.
Мой макет имеет высоту всех элементов в wrap_content
.
В MyFragmentDialog
я даже не могу найти метод, который подразумевал бы, что его можно использовать для установки высоты FragmentDialog
. Что мне не хватает? Как сделать диалоговое окно "Диалоговое окно" соответствующим ему?
Метод DialogFrament
onCreateView
:
@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>
Спасибо,
Ответы
Ответ 1
Оказывается, проблема с LinearLayout
, несмотря на установку высоты на ней, в DialogFragment
кажется, что требуется больше места, чем я хочу. Я переключил макет на RelativeLayout
, содержимое Dialog
, похоже, изменило размер, чтобы соответствовать содержимому.
Ответ 2
Вы можете заставить его работать со всеми типами макетов при добавлении размера макета в onResume вашего диалогаFragment:
@Override
public void onResume()
{
super.onResume();
Window window = getDialog().getWindow();
window.setLayout(300, 300);
window.setGravity(Gravity.CENTER);
}
Ответ 3
Я совершил прыжок в Apache Dialog, поэтому я, конечно, не уверен, но вы можете попытаться вызвать getWindow в диалоговом окне, а затем вызвать setLayout (ширина, высота)
getDialog().getWindow().setLayout(300,300);
Ответ 4
Я не вижу верхнюю часть вашего тега LinearLayout, но я видел случаи, когда все, что было необходимо, было
android:orientation="vertical"
XML-атрибут. Это может вызвать головную боль, но, к счастью, легко исправить.