Android充分利用系统状态栏,达到全屏显示,fitSystemWindow失效问题

先是配置Activity的style设置

<style name="Full_Used_status_bar" parent="Theme.AppCompat.Light.NoActionBar">
	"android:windowTranslucentStatus">true
	"android:statusBarColor">@android:color/transparent
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失效问题_第1张图片
fitsystemwindow:false时:按钮什么的都跑到状态栏下面去了

当fitsystemwindow:true时:除了背景以外的的都从状态栏下界开始布局

最后:
notice:别忘了给状态栏字题设置颜色:只有亮色和暗色两种,当背景颜色偏亮时设置成暗色,当背景颜色偏暗时设置成亮色。
设置成暗色:

window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)

设置成亮色:

window.getDecorView().setSystemUiVisibility(0)

Alt

你可能感兴趣的:(Android,AndroidUI进阶)