2017-08-10 174 views
1

我在我的活動中使用BottomSheetDialogFragment時,對話框在縱向模式下顯示全高,但在切換到橫向模式時不顯示。BottomSheetDialogFragment在橫向模式下不顯示全高

Portrait Mode

Landscape Mode

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     CustomBottomSheetDialog customBottomSheetDialog = new CustomBottomSheetDialog(); 
     customBottomSheetDialog.show(getSupportFragmentManager(),customBottomSheetDialog.getTag()); 
    } 
} 

CustomBottomSheetDialog

public class CustomBottomSheetDialog extends BottomSheetDialogFragment { 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     return View.inflate(getContext(), R.layout.view_config, null); 
    } 
} 

在橫向模式下CustomBottomSheetDialog佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:background="#fdf107" 
       android:layout_height="wrap_content"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="196dp" 
     android:gravity="center" 
     android:textColor="@color/colorAccent" 
     android:text="BottomSheetDialogFragment"/> 

</LinearLayout> 

,我要拖BottomSheetDialogFragment看到整個內容。

+0

發現它的預期行爲(https://issuetracker.google.com/issues/37083487)。引用「材料設計指南」指出,底部片材應該看到底部片材上方區域高度爲19:6的高度。由於您的橫向屏幕比16:9短,因此它會在規格中的最小高度上偷看。 「 – Bracadabra

回答

2

此問題的解決方案是。

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      BottomSheetDialog dialog = (BottomSheetDialog) getDialog(); 
      FrameLayout bottomSheet = (FrameLayout) 
      dialog.findViewById(android.support.design.R.id.design_bottom_sheet); 
      BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); 
      behavior.setState(BottomSheetBehavior.STATE_EXPANDED); 
      behavior.setPeekHeight(0); 
     } 
    }); 
} 
+0

感謝您的解決方案 –

+1

一旦事件被觸發,不要忘記註銷監聽器。 –

相關問題