天天看點

Android充分利用系統狀态欄,達到全屏顯示,fitSystemWindow失效問題

先是配置Activity的style設定

<style name="Full_Used_status_bar" parent="Theme.AppCompat.Light.NoActionBar">
	<item name="android:windowTranslucentStatus">true</item>
	<item name="android:statusBarColor">@android:color/transparent</item>
</style>
           

android:windowTranslucentStatus 允許把狀态欄當作布局位置使用

android:statusBarColor 是決定狀态欄背景顔色,當設定為透明時能達到預期效果

重寫ViewGroup或者索要用的每一個Layout,

目的是:當新的子view添加到Layout時新的任務把inset傳遞給每一個子view

such as the FrameLayout bellow:

public class MyFramLayout extends FrameLayout {
    public MyFramLayout(@NonNull Context context) {
        super(context);
    }

    public MyFramLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyFramLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyFramLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    public void init(){
        setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                requestApplyInsets();
            }

            @Override
            public void onChildViewRemoved(View parent, View child) {

            }
        });
    }

    @Override
    public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
        for(int index=0;index<getChildCount();index++){
            getChildAt(index).dispatchApplyWindowInsets(insets);
        }
        return insets;
    }
}
           

下面是ConstraintLayout的重寫:

public class MyConstraintLayout extends ConstraintLayout {
    public MyConstraintLayout(Context context) {
        super(context);
        init();
    }

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

    public MyConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public void init(){
        setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                requestApplyInsets();
            }

            @Override
            public void onChildViewRemoved(View parent, View child) {

            }
        });
    }

    @Override
    public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
        for(int index=0;index<getChildCount();index++){
            getChildAt(index).dispatchApplyWindowInsets(insets);
        }
        return insets;
    }
}
           

fitSystemWindow的詳細介紹:

Android充分利用系統狀态欄,達到全屏顯示,fitSystemWindow失效問題

fitsystemwindow:false時:按鈕什麼的都跑到狀态欄下面去了

Android充分利用系統狀态欄,達到全屏顯示,fitSystemWindow失效問題

當fitsystemwindow:true時:除了背景以外的的都從狀态欄下界開始布局

Android充分利用系統狀态欄,達到全屏顯示,fitSystemWindow失效問題

最後:

notice:别忘了給狀态欄字題設定顔色:隻有亮色和暗色兩種,當背景顔色偏亮時設定成暗色,當背景顔色偏暗時設定成亮色。

設定成暗色:

設定成亮色:

Android充分利用系統狀态欄,達到全屏顯示,fitSystemWindow失效問題