先是配置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的详细介绍:
fitsystemwindow:false时:按钮什么的都跑到状态栏下面去了
当fitsystemwindow:true时:除了背景以外的的都从状态栏下界开始布局
![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20200120195745375.png =200x?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzA2NjA5Nw==,size_16,color_FFFFFF,t_70,=200x50)
最后:
notice:别忘了给状态栏字题设置颜色:只有亮色和暗色两种,当背景颜色偏亮时设置成暗色,当背景颜色偏暗时设置成亮色。
设置成暗色:
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
设置成亮色:
window.getDecorView().setSystemUiVisibility(0)
@Sadam
发布了71 篇原创文章 · 获赞 2 · 访问量 2367
私信
关注