2016-02-25 15 views
26

私はSupport Library 23.2で追加された新しいBottomSheetDialogを使用していますが、ダイアログのデフォルトの高さを変更したいと思います。最初の高さを制御するbehavior_peekHeight属性と関連付ける必要があることはわかっていますが、BottomSheetBehaviorに直接アクセスできない場合は、BottomSheetDialogにどのように設定しますか?もう一つの方法は、BottomSheetDialogFragmentを継承しているBottomSheetDialogのデフォルトの高さを変更するにはどうすればよいですか?

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="bottomSheetDialogTheme">@style/AppBottomSheetDialogTheme</item> 
</style> 

<style name="AppBottomSheetDialogTheme" 
     parent="Theme.Design.Light.BottomSheetDialog"> 
    <item name="bottomSheetStyle">@style/AppModalStyle</item> 
</style> 

<style name="AppModalStyle" 
     parent="Widget.Design.BottomSheet.Modal"> 
    <item name="behavior_peekHeight">@dimen/custom_peek_height</item> 
</style> 

答えて

40

あなたはbottomSheetStyle属性のbehavior_peekHeightを上書きし、あなたの活動にbottomSheetDialogThemeを設定することができます。ビューツリーに移動すると、BottomSheetDialogがコンテンツビューをラップアップする動作を取得できます。より多くのレイアウトパスが必要なので、それは本当に良い解決策ではありません。一番下のシートの状態がSTATE_HIDDENであるときには、ライブラリで提供されている実装に明確に違反していなければダイアログを閉じる必要があります。

ピーク時の高さをプログラムで設定した後、コンテンツビューでは実際に別のレイアウトパスであるrequestLayout()を呼び出す必要があります。

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment { 


    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() { 

     @Override 
     public void onStateChanged(@NonNull View bottomSheet, int newState) { 
      setStateText(newState); 
      if (newState == BottomSheetBehavior.STATE_HIDDEN) { 
       dismiss(); 
      } 

     } 

     @Override 
     public void onSlide(@NonNull View bottomSheet, float slideOffset) { 
     } 
    }; 

@Override 
    public void setupDialog(Dialog dialog, int style) { 
     super.setupDialog(dialog, style); 
     View contentView = View.inflate(getContext(), R.layout.bottom_sheet_dialog_content_view, null); 
     dialog.setContentView(contentView); 
     mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent())); 
     if (mBottomSheetBehavior != null) { 
      mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);  
      mBottomSheetBehavior.setPeekHeight(peekHeight); 
      contentView.requestLayout(); 
     } 
} 
+1

これは機能しませんTheme.AppCompat.Light.NoActionBarを親のレイアウトとして使用する –

+1

behavior_peekHeightは、値が208dpより大きい場合にのみ有効です。何故ですか? –

+4

@AmJay - バージョン23.2.1で修正されるはずです。更新してください! – ianhanniballake

4

し、コンテンツビューを設定する際の制御方法とがあります。

+0

この時点で、あなたはまだ使用しているよりも多くの 'BottomSheetDialog'を置き換えてしまっています。 – ianhanniballake

+0

@ianhanniballake私の最初の意図は、単にBottomSheetCallback'の内部インスタンスBottomSheetDialogをバイパスして、単にダイアログを閉じるだけで、もっと自分自身でコントロールすることでした。このシナリオでも、行動のインスタンスを利用することができます。 –

+0

注意:あなたのロジックを単純化するヘルパーメソッド 'BottomSheetBehavior.from(View)'があります( 'IllegalArgumentException'sをスローします)。 – ianhanniballake

9

のstyles.xml

<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog"> 
    <item name="bottomSheetStyle">@style/bottomSheetStyleWrapper</item> 
</style> 

<style name="bottomSheetStyleWrapper" parent="Widget.Design.BottomSheet.Modal"> 
    <item name="behavior_peekHeight">500dp</item> 
</style> 

BottomSheetDialog dialog = new BottomSheetDialog(this, R.style.BottomSheetDialog); 
      dialog.setContentView(R.layout.layout_bottom_sheet); 
      dialog.show(); 
+0

これは私のために働いていない、下のシートはすべての画面の高さをとります。 – Shirane85

+0

behavior_peekHeightを変更してください。 – Fang

+0

私は明らかにそうしました。しかし、私は何を置くのかは関係ありません。 – Shirane85

12

あなたはニックとのLiTaOのソリューションを組み合わせることで、あなたのコード内のBottomSheetBehavior

BottomSheetDialog dialog = new BottomSheetDialog(content); 
. 
. 
. 
dialog.setContentView(view); 
BottomSheetBehavior mBehavior = BottomSheetBehavior.from((View) view.getParent()); 
mBehavior.setPeekHeight(your dialog height) 
dialog.show(); 
0

を使用することができ、これは我々が何をすべきかの完全なバージョンです:

BottomSheetDialog bottomSheet = new BottomSheetDialog(context); 
View view = View.inflate(context, R.layout.your_action_sheet, null); 
bottomSheet.setContentView(view); 
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(((View) view.getParent())); 
bottomSheetBehavior.setPeekHeight(1000); 
bottomSheet.show(); 
関連する問題