2015-10-15 14 views
5

を削除します。 fitsSystemWindowsを削除すると、パディングが戻ってきます。どうして? fitsSystemWindowsとパディングをどうすれば保持できますか?fitsSystemWindowsは、私は私のレイアウトでこれを持ってパディング

+0

この記事は、[なぜ私はfitsSystemWindowsになりたいのですか?](https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.kpokdt33j)で取り上げられています。 – Sufian

答えて

4

fitsSyatemWindows属性オーバーライドパディングがレイアウトを適用しました。だから、パディングを適用する

、あなたのRelativeLayoutに1つのラッパーのレイアウトを作成し、それにfitsSystemWindows属性を追加し、子供RelativeLayoutpadding必要があります。

<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を)レイアウトクラスを拡張し、(API> = 20の場合)またはonApplyWindowInsets()(API < 20用)fitSystemWindows()でトップのパディングを削除する必要があります。

関連する問題