Activityに複数のFragment切り替えを追加し、透明なステータスバーの後、上部がステータスバーの問題に入ります

1713 ワード

stackoverflowのウェブサイトの解決方法は転載します:安久哲(翻訳)
  • が発生した理由は、最初のFragmentがActivityに追加されたとき、ActivityはfitsystemWindowsのサブレイアウトがステータスバーを予約するための空間を探し、実際にはpaddingを設定し、他のFragmentがActivityに追加されたとき、ステータスバー空間の適合が一度消費されたため、Activityは再びこのpaddingを追加しないからである.そのため、FrameLayoutをカスタマイズし、ステータスバーのスペースが適切なタイミングとその適切なイベントの配布を書き換える必要があります.次はコアコードです.
  • public class WindowInsetsFrameLayout extends FrameLayout {
     
        public WindowInsetsFrameLayout(Context context) {
            this(context, null);
        }
     
        public WindowInsetsFrameLayout(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
     
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public WindowInsetsFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context,attrs,defStyleAttr);
            setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
                @Override
                public void onChildViewAdded(View parent, View child) {
                    requestApplyInsets();
                }
     
                @Override
                public void onChildViewRemoved(View parent, View child) {
     
                }
            });
        }
        
        @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
        @Override
        public WindowInsets onApplyWindowInsets(WindowInsets insets) {
            int childCount = getChildCount();
            for (int index = 0; index < childCount; index++)
                getChildAt(index).dispatchApplyWindowInsets(insets);
            return insets;
        }
    }
    
  • FragmentはFrameLayoutというコンテナに追加されたので、レイアウト階層の変更の傍受を設定し、Fragmentが追加されるとrequestApplyInsets()メソッドでActivityをステータスバー空間の適合を再開させます.FrameLayoutには他のFragmentがあるので、onApplyWindowInsetsメソッドを書き換え、そのサブViewを遍歴する必要があります.ステータスバースペースの適合イベントを1つずつ配布します.