2015-10-15 21 views
5

ho questo nel mio layout:fitsSystemWindows rimuove imbottitura

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/primary" 
    android:paddingLeft="32dp" 
    android:paddingRight="32dp" 
    android:fitsSystemWindows="true"> 

    ... 

</RelativeLayout> 

Ma non c'è paddingLeft o paddingRight nella mia app. Quando rimuovo fitsSystemWindows, il padding ritorna. Perché? Come posso mantenere fitsSystemWindows e il padding?

+0

Questo è stato risolto nell'articolo - [Perché dovrei voler installare SystemWindows?] (Https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.kpokdt33j). – Sufian

risposta

4

fitsSyatemWindows attributo ignora il riempimento applicato al layout.

Quindi, per applicare l'imbottitura, è necessario creare un layout wrapper per il vostro RelativeLayout e aggiungere l'attributo fitsSystemWindows ad esso, e padding a figlio RelativeLayout.

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/primary" 
    android:fitsSystemWindows="true">  //this is container layout 

    <RelativeLayout 
     android:paddingLeft="32dp" 
     android:paddingRight="32dp" 
     ..... >       //now you can add padding to this 

      ..... 

    </RelativeLayout> 
</RelativeLayout> 
+3

Non ignora il riempimento di per sé, è che fitsSystemWindows ** sostituisce ** il padding con un riempimento sufficiente per rendere la vista adatta alle finestre di sistema. – ianhanniballake

+0

@ianhanniballake aw! Grazie per aver corretto il mio errore :) Migliorare la risposta – Apurva

4

mi limiterò a aggiungi a qui nel caso in cui qualcuno ha bisogno di rimuovere il padding superiore quando si utilizza fitSystemWindows. Questo può essere il caso quando si usano le barre di azione personalizzate, DrawerLayout/NavigationView e/o frammenti.

public class CustomFrameLayout extends FrameLayout { 
    public CustomFrameLayout(Context context) { 
     super(context); 
    } 

    public CustomFrameLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected boolean fitSystemWindows(Rect insets) { 
     // this is added so we can "consume" the padding which is added because 
     // `android:fitsSystemWindows="true"` was added to the XML tag of View. 
     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN 
       && Build.VERSION.SDK_INT < 20) { 
      insets.top = 0; 
      // remove height of NavBar so that it does add padding at bottom. 
      insets.bottom -= heightOfNavigationBar; 
     } 
     return super.fitSystemWindows(insets); 
    } 

    @Override 
    public WindowInsets onApplyWindowInsets(WindowInsets insets) { 
     // executed by API >= 20. 
     // removes the empty padding at the bottom which equals that of the height of NavBar. 
     setPadding(0, 0, 0, insets.getSystemWindowInsetBottom() - heightOfNavigationBar); 
     return insets.consumeSystemWindowInsets(); 
    } 

} 

dobbiamo estendere la classe di layout (FrameLayout nel mio caso) e rimuovere l'imbottitura superiore nella fitSystemWindows() (per API < 20) o onApplyWindowInsets() (per API> = 20).