2016-04-05 46 views
9

最近android.support.design.widget.BottomSheetDialogFragmentを使用しました。私はGoogleの連絡先アプリに似た何かをしたかったので、BottomSheetはツールバーとステータスバーの上に重ねることができます。あなたはアクティビティのツールバーがまだ表示されて見ることができるように My ImplementationbottomSheetDialogをステータスバーの全画面に表示

:私はこれを実装するためにBottomSheetDialogFragmentを使用する場合しかし、それはこれに判明します。ここBottomSheetDialogFragmentの私のコードは次のとおりです。

public class KeyDetailFragment extends BottomSheetDialogFragment { 
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() { 
     @Override 
     public void onStateChanged(@NonNull View bottomSheet, int 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(getActivity(), R.layout.sheet_key, null); 
     dialog.setContentView(contentView); 
     View parent = (View) contentView.getParent(); 
     parent.setFitsSystemWindows(true); 
     BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent); 
     contentView.measure(0, 0); 
bottomSheetBehavior.setPeekHeight(contentView.getMeasuredHeight()); 

     CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams(); 
     if (params.getBehavior() instanceof BottomSheetBehavior) { 
       ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
     } 
     params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; 
     parent.setLayoutParams(params); 
    } 
} 

私はソースと呼ばれ、私は属性が私を興味が見つかりました:

private static int getThemeResId(Context context, int themeId) { 
    if (themeId == 0) { 
     // If the provided theme is 0, then retrieve the dialogTheme from our theme 
     TypedValue outValue = new TypedValue(); 
     if (context.getTheme().resolveAttribute(
       R.attr.bottomSheetDialogTheme, outValue, true)) { 
      themeId = outValue.resourceId; 
     } else { 
      // bottomSheetDialogTheme is not provided; we default to our light theme 
      themeId = R.style.Theme_Design_Light_BottomSheetDialog; 
     } 
    } 
    return themeId; 
} 

ここ属性bottomSheetDialogThemeがボトムシートのスタイルを変更することがありますが、私はしないでくださいそれを変更する方法を知っていて、これが機能するかどうかは疑問です。誰かがツールバーとステータスバーをオーバーレイすることができるということについて私に解決策を教えてもらえますか?

+0

同じ問題は、これに対する解決策を見つけましたか? –

+0

@ nipun.birlaいいえ、まだ3ヶ月経っていますが、まだ見つかりませんでした –

+0

私は理由が仰角だと思います。これを確認してください:http://stackoverflow.com/questions/35711808/android-bottomsheet-is-hinding-under-the-toolbar –

答えて

3

この問題の解決策を見つけることができませんでしたが、同じ目的を果たすのに役立つ代替案を提案することができます。 http://www.hidroh.com/2016/06/17/bottom-sheet-everything/

この記事では、ボトムシートのアクティビティを作成し、それにバックドロップシャドウを追加する方法について説明します。

+0

与えられたリファレンスは確かに私の目的の別の方法です、バックドロップシャドーを加えることは本当に面倒です。 –

11

これを試してください。わたしにはできる。

@Override 
public void setupDialog(Dialog dialog, int style) { 
    super.setupDialog(dialog, style); 
    View inflatedView = View.inflate(getContext(), R.layout.fragment_coupon, null); 
    dialog.setContentView(inflatedView); 


    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) inflatedView.getParent()).getLayoutParams(); 
    CoordinatorLayout.Behavior behavior = params.getBehavior(); 

    if (behavior != null && behavior instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
    } 

    View parent = (View) inflatedView.getParent(); 
    parent.setFitsSystemWindows(true); 
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent); 
    inflatedView.measure(0, 0); 
    DisplayMetrics displaymetrics = new DisplayMetrics();  getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int screenHeight = displaymetrics.heightPixels; 
    bottomSheetBehavior.setPeekHeight(screenHeight); 

    if (params.getBehavior() instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
    } 

    params.height = screenHeight; 
    parent.setLayoutParams(params); 
} 
+2

はうまく動作します。 –

+0

ありがとう@LOG_TAG! :) – Vaigunth

+1

この解決策に注意しなければならないことは、displayMetrics.heightPixelsには通知トレイの高さが含まれているため、これが少し下を切り取ることです。これを使用して通知トレイの高さを計算しました。 'private int getStatusBarHeight(){ Rect Rectangle = new Rect(); ウィンドウウィンドウ= getActivity()。getWindow(); window.getDecorView()。getWindowVisibleDisplayFrame(rectangle); return rectangle.top; } ' –

0

これが一番簡単だったし、私のために働いた、ちょうどBottomSheetDialogを拡張し、設定BottomSheetBehaviorはをBottomSheetBehavior.STATE_EXPANDED

リトルハックは、Androidのサポート設計ライブラリから取得されたレイアウト名android.support.design.R.id.design_bottom_sheetある

class BottomSheetDialogExpanded(context: Context) : BottomSheetDialog(context) { 

    private lateinit var mBehavior: BottomSheetBehavior<FrameLayout> 

    override fun setContentView(view: View) { 
     super.setContentView(view) 
     val bottomSheet = window.decorView.findViewById<View>(android.support.design.R.id.design_bottom_sheet) as FrameLayout 
     mBehavior = BottomSheetBehavior.from(bottomSheet) 
     mBehavior.state = BottomSheetBehavior.STATE_EXPANDED 
    } 

    override fun onStart() { 
     super.onStart() 
     mBehavior.state = BottomSheetBehavior.STATE_EXPANDED 
    } 
} 
関連する問題