2015-10-15 18 views
5

mam to w moim układzie:fitsSystemWindows usuwa wyściółka

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

Ale nie ma paddingLeft lub paddingRight w mojej aplikacji. Po usunięciu fitsSystemWindows pojawia się dopełnienie. Czemu? Jak mogę zachować fitsSystemWindows i dopełnienie?

+0

Zostało to omówione w artykule - [Dlaczego chciałbym dopasować system Windows?] (Https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.kpokdt33j). – Sufian

Odpowiedz

4

fitsSyatemWindows atrybut zastępuje wypełnienie zastosowało układ.

więc stosować wyściółkę, należy utworzyć jeden układ otoki do swojej RelativeLayout i dodać atrybut fitsSystemWindows do niego i padding do dziecka 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

Nie jest tak, że ignoruje dopełnienie jako takie, jest to, że fitsSystemWindows ** zastępuje ** dopełnienie z dostateczną ilością dopełnienia, aby uzyskać odpowiednie okna systemu. – ianhanniballake

+0

@ianhanniballake aw! Dzięki za naprawienie mojego błędu :) Poprawienie odpowiedzi – Apurva

4

dodam tylko to tutaj na wypadek gdyby ktoś musi zdjąć górną wyściółkę przy użyciu fitSystemWindows. Może tak być w przypadku używania niestandardowych pasków akcji, DrawerLayout/NavigationView i/lub fragmentów.

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

} 

Musimy rozszerzyć klasę Layout (FrameLayout w moim przypadku) i zdjąć górną wyściółkę w fitSystemWindows() (dla API < 20) lub (dla API onApplyWindowInsets()> = 20).