2015-10-15 73 views
5

我在佈局有這樣的:fitsSystemWindows去除填充

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

但在我的應用程序沒有paddingLeftpaddingRight。當我刪除fitsSystemWindows時,填充返回。爲什麼?我怎樣才能保持fitsSystemWindows和填充?

+0

這已在文章中解決 - [我爲什麼要配合SystemWindows?](https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.kpokdt33j)。 – Sufian

回答

4

fitsSyatemWindows屬性覆蓋填充應用了佈局。

所以應用填充,你應該創建一個封裝佈局,以你的RelativeLayout並添加fitsSystemWindows屬性給它,並padding兒童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

這並不是說它忽略填充本身,而是因爲只有足夠的填充才能使視圖適合系統窗口,所以fitsSystemWindows **將覆蓋**填充。 – ianhanniballake

+0

@ianhanniballake aw!感謝您解決我的錯誤:)改善答案 – Apurva

4

我只要加入這個位置的情況下,任何人都需要使用當fitSystemWindows卸下頂部填充。使用自定義操作欄,DrawerLayout/NavigationView和/或片段時可能會出現這種情況。

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

} 

我們不得不延長佈局類(的FrameLayout在我的情況),併除去在fitSystemWindows()頂部填充(對於API < 20)或onApplyWindowInsets()(對於API> = 20)。