十二、Android的沉浸与动态沉浸

Window的各种效果

默认的效果

导航栏显示,内容处于导航键上方
style

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

在这里插入图片描述

fitSystemWindow

单独使用fitSystemWindow是没有效果的,如下图
在这里插入图片描述

fitSystemWindow配合透明状态栏和导航栏


    <style name="TransparentStyle" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsTranslucent">true</item>
        <!--设置导航栏/状态栏窗口color为透明-->
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--        <item name="android:windowNoTitle">true</item>-->



        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <!-- Customize your theme here. -->
        <!--        <item name="colorPrimary">@color/colorPrimary</item>-->
        <!--        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>-->
        <!--        <item name="colorAccent">@color/colorAccent</item>-->

    </style>

在这里插入图片描述

FitsystemWindow内容延申到状态栏


public class NormalFitSystemWindowTransparentCoverNavigationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_fitsystemwindow_transparent_content_tonavigation);
        RelativeLayout backgroundLayout = findViewById(R.id.background_layout);
        FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) backgroundLayout.getLayoutParams();
        layoutParams.bottomMargin = - getRealNavigationBarHeight(this);
        layoutParams.topMargin = - getStatusBarHeight();
        backgroundLayout.setLayoutParams(layoutParams);


    }





    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    public static int getRealNavigationBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return resources.getDimensionPixelSize(resourceId);
        }
        return 0;
    }



}

在这里插入图片描述

最后有一个小技巧补充一下,改变状态栏图标的颜色:
已经废弃:

     //切换到浅色状态栏模式,黑字
            getWindow().getDecorView()
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

    //切换到深色模式,白字
             getWindow().getDecorView()
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

新版本使用:

//状态栏到高亮模式,白色false ,黑色true
        val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView)
        windowInsetsController?.isAppearanceLightStatusBars = false

Demo地址:

https://github.com/HumorSmith/UsefulDemoZip/blob/master/windowFit.rar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值