5

Ho uno Activity che viene visualizzato a schermo intero. Ciò funziona perfettamente con molti layout che ho provato, tranne quando lo CoordinatorLayout è la radice ViewGroup. Lo stesso CoordinatorLayout ha larghezza e altezza impostate su match_parent e occupa tutto lo schermo come dovrebbe. Ma le viste secondarie che dovrebbero avere le stesse dimensioni dello CoordinatorLayout vengono posizionate come se la barra di navigazione fosse ancora visibile.CoordinatorLayout bambini non sono a schermo intero

Illustration of the problem

C'è un modo per rendere i punti di vista dei bambini ridimensionare con il CoordinatorLayout? Ovviamente fitSystemWindows non cambia nulla in quanto ciò è probabilmente causato dall'implementazione CoordinatorLayout, altri ViewGroups funzionano bene. Ho provato a creare la classe personalizzata Behavior ma non ho avuto successo con quello.

utilizzare questo codice per fare i miei Activity fullscreen:

@Override 
protected void onResume() { 
    super.onResume(); 
    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
      | View.SYSTEM_UI_FLAG_FULLSCREEN 
      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 
    getWindow().getDecorView().setSystemUiVisibility(uiOptions); 
} 

Questo è un layout semplice utilizzato per generare l'immagine:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:background="#F00"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     android:scaleType="centerCrop" 
     android:src="@drawable/background_sky_light" /> 

</android.support.design.widget.CoordinatorLayout> 

risposta

0

ho avuto un problema simile e ha dovuto incidere un po ' di una soluzione. Invece di un ImageView, la mia vista secondaria CoordinatorLayout è per lo streaming di un video. Dal momento che sto trasmettendo un video in streaming durante la rotazione, ho bisogno di guardare per configChanges e sovrascrivere onConfigurationChanged. Questo potrebbe non funzionare per te se non vuoi sovrascrivere onConfigurationChanged, ma potrebbe funzionare. Come ho detto, è un po 'un trucco, ma funziona per me.

Finisco per espandere completamente la barra degli strumenti, memorizzando l'offset, quindi nascondendolo (trattandolo come una vista standard). Quindi quando ruoto indietro, lo ridimensiono a qualsiasi sia l'offset quando l'utente ha ruotato il dispositivo.

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     showToolbar(); 
     mViewPager.setVisibility(View.VISIBLE); 

    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     hideToolbar(); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     mViewPager.setVisibility(View.GONE); 
    } 
} 

public void hideToolbar() 
{ 
    setCurrentOffset(); 
    expandToolbarToHeight(0); 
    findViewById(R.id.toolbar).setVisibility(View.GONE); 
} 

public void showToolbar() 
{ 
    expandToolbarToHeight(oldOffset); 
    findViewById(R.id.toolbar).setVisibility(View.VISIBLE); 
} 

public void setCurrentOffset() { 
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); 
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); 
    if (behavior != null) { 
     oldOffset = behavior.getTopAndBottomOffset(); 
    } 
} 

public void expandToolbarToHeight(int height) { 
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams(); 
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); 
    if (behavior != null) { 
     behavior.setTopAndBottomOffset(height); 
     behavior.onNestedPreScroll(mCoordinatorLayout, mAppBarLayout, null, 0, 1, new int[2]); 
    } 
} 
-1

si può tentare di rimuovere android:fitsSystemWindows="true" da ImageView, e cambiare onResume() come questo:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    .... 
    mRootView = (CoordinatorLayout) findViewById(R.id.coordinatorLayout); 
    .... 
} 

public void hideBars() { 
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 
      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 
      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
      | View.SYSTEM_UI_FLAG_FULLSCREEN 
      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); 
    mRootView.setFitsSystemWindows(false); 
} 

Il mRootView.setFitsSystemWindows(false); è importante per la vista dei bambini montaggio in schermo intero.

Senza questa chiamata, il layout appare come se la barra di stato e la barra di navigazione esistessero ancora. Come si vede nella foto qui sotto:

Screenshot without setFitsSystemWindows(false)

E con questo invito:

Screenshot with setFitsSystemWindows(false)

Il motivo, credo, è che quando fitsSystemWindows è vero, il CoordinatorLayout riserverà spazio per lo stato barra e barra di navigazione per lavorare con altri widget che disegnano lo sfondo delle barre stesse. Ma quando nascondiamo le barre dell'interfaccia utente del sistema, non è necessario riservare spazio, pertanto è necessario comunicare allo CoordinatorLayout di rilasciare gli spazi per le altre viste secondarie.

seguente è il layout della schermata

di layout per l'attività:

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/coordinatorLayout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/black" 
    android:fitsSystemWindows="true" 
    tools:context="org.hamster.carz.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/frag_container"/> 
    <!-- Just a empty FrameLayout --> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@mipmap/car_add" /> 

</android.support.design.widget.CoordinatorLayout> 

Il frammento negli screenshot qui sopra:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:orientation="vertical"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="2dp" 
      android:layout_weight="1" 
      android:background="@drawable/controller_button"/> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:background="@drawable/controller_button"/> 
    </LinearLayout> 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="12dp" 
     android:layout_weight="1"> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab_disconnect" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_margin="@dimen/fab_margin" 
      android:src="@mipmap/ic_clear" 
      app:backgroundTint="@color/colorAccentDark"/> 
    </FrameLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:orientation="vertical"> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginBottom="2dp" 
      android:layout_weight="1" 
      android:background="@drawable/controller_button"/> 

     <Button 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_marginTop="2dp" 
      android:layout_weight="1" 
      android:background="@drawable/controller_button"/> 
    </LinearLayout> 

</LinearLayout> 

Spero che questo aiuti.

1

cambiamento Android: fitsSystemWindows a false se si utilizza coodinatorlayout android:fitsSystemWindows="false "