2012-01-22 36 views
12

Tôi vừa mới sử dụng Hộp thoại đầu tiên bằng DialogFragment. Mọi thứ đều hoạt động tốt ngoại trừ tôi không thể lấy Hộp thoại để bọc bố cục của nó. Bố cục của tôi có chiều cao của tất cả các phần tử là wrap_content.DialogFragment đặt chiều cao của Hộp thoại

Trong MyFragmentDialog Tôi thậm chí không thể tìm thấy phương pháp ngụ ý rằng nó có thể được sử dụng để đặt chiều cao của FragmentDialog. Tôi đang thiếu gì? Làm thế nào để làm cho một DialogFragment phù hợp với nội dung của nó?

onCreateView Phương pháp này DialogFrament 's:

@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; 
} 

Đây là cách bố trí:

<?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> 

Cảm ơn,

Trả lời

29

Hóa ra là một vấn đề với LinearLayout mặc dù thiết lập một chiều cao vào nó, trong DialogFragment nó dường như được tham gia không gian nhiều hơn tôi muốn nó. Tôi đã chuyển bố cục thành một nội dung RelativeLayout nội dung Dialog dường như đổi kích thước để vừa với nội dung.

+0

hey u có thể đăng các giải pháp? u có đặt chiều cao cho bố cục tương đối hoặc wrap_content không? Chiều rộng – Ads

+0

là 'match_parent', chiều cao là' wrap_content'. Hãy cho tôi biết nếu bạn vẫn cần tôi đăng giải pháp. – Ali

+0

Cảm ơn Ali.i đã thực hiện công việc around.i bọc bố cục hiện tại với Bố cục tương đối có chiều rộng và chiều cao là match_parent.Sure, u có thể đăng câu trả lời sẽ hữu ích cho người khác, nếu cần. – Ads

4

tôi lấy một bước nhảy vọt vào api Dialog vì vậy tôi chắc chắn là không chắc chắn nhưng bạn có thể thử gọi getWindow trên t ông hộp thoại và sau đó gọi setLayout (chiều rộng, chiều cao)

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

Cảm ơn Bram, nhưng nó hóa ra là một vấn đề với 'LinearLayout' tôi chuyển nó thành một 'RelativeLayout' và nội dung' Dialog' dường như thay đổi kích thước để phù hợp với nội dung. – Ali

0

Tôi không thấy phần trên của thẻ LinearLayout của bạn, nhưng tôi đã nhìn thấy trường hợp tất cả những gì là cần thiết là XML thuộc tính

android:orientation="vertical" 

. Nó có thể gây đau đầu nhưng may mắn là dễ sửa chữa.

5

Bạn có thể làm cho nó làm việc với tất cả các loại bố trí trong việc thêm kích thước của bố trí trong onResume của dialogFragment của bạn:

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

Trong số 100 giây giải pháp tôi đã tìm thấy trên SO cho vấn đề ngu ngốc này, đây là một trong đó đã làm việc và giữ cho tôi lành mạnh thêm một ngày. – Gyome

+0

đã làm việc cho tôi :) –

+0

@Gyome vui lòng tham khảo câu hỏi này - https: // stackoverflow.com/questions/45494644/how-to-set-the-height-of-the-hộp-đoạn-chính xác-trong-android –

Các vấn đề liên quan