2016-04-05 34 views
9

Recentemente ho utilizzato android.support.design.widget.BottomSheetDialogFragment. Volevo fare qualcosa che fosse simile all'app di contatto Google, il suo BottomSheet può sovrapporre la barra degli strumenti e la barra di stato. Tuttavia, quando uso il BottomSheetDialogFragment per implementare questo, si scopre questo: My ImplementationImposta bottomSheetDialog a schermo intero sulla barra di stato

Come si può vedere la barra degli strumenti dell'attività è ancora visibile. Ecco il mio codice del 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); 
    } 
} 

ho fatto riferimento alla fonte e ho trovato un attributo mi interessa:

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

l'attributo bottomSheetDialogTheme qui può cambiare lo stile del foglio di fondo, ma io no sapere come cambiarlo, e dubito che questo funzionerebbe. Qualcuno può darmi una soluzione per raggiungerlo che può sovrapporre la barra degli strumenti e la barra di stato?

+0

Stesso problema, hai trovato qualche soluzione a questo? –

+0

@ nipun.birla No, non l'ho ancora trovato sebbene fosse passato per circa 3 mesi –

+0

Penso che il motivo sia l'elevazione. Verifica questo: http://stackoverflow.com/questions/35711808/android-bottomsheet-is-hinding-under-the-toolbar. –

risposta

3

Non è stato possibile trovare la soluzione a questo problema, ma può suggerire un altro che mi ha aiutato a servire allo stesso scopo. Ecco il riferimento: http://www.hidroh.com/2016/06/17/bottom-sheet-everything/

L'articolo illustra la creazione di un'attività del foglio di sotto e l'aggiunta dell'ombra dello sfondo.

+0

Il riferimento dato è sicuramente un modo alternativo al mio scopo, aggiungere un'ombra sullo sfondo è davvero complicato. –

11

Prova questo. Per me funziona.

@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

funziona bene, ottimo lavoro! –

+0

Grazie @LOG_TAG! :) – Vaigunth

+1

Una cosa da notare con questa soluzione è il displayMetrics.heightPixels includerà l'altezza del vassoio di notifica in modo da tagliare leggermente il fondo. L'ho usato per calcolare l'altezza della barra delle notifiche. 'private int getStatusBarHeight() { Rettangolo rettangolo = nuovo Rettangolo(); Window window = getActivity(). GetWindow(); window.getDecorView(). GetWindowVisibleDisplayFrame (rettangolo); return rectangle.top; } ' –

0

Questo è stato facile e ha lavorato per me, basta estendere BottomSheetDialog e impostare BottomSheetBehavior a BottomSheetBehavior.STATE_EXPANDED

piccolo hack è nome del layout android.support.design.R.id.design_bottom_sheet è preso dalla libreria di progettazione supporto Android

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