Android沉浸状态栏的简单应用以及包含fragment沉浸冲突的解决方法

前言:Google从android kitkat(Android 4.4)开始,给我们开发者提供了一套能透明的系统ui样式给状态栏和导航栏,这样的话就不用向以前那样每天面对着黑乎乎的上下两条黑栏了,还可以调成跟Activity一样的样式,形成一个完整的主题,和IOS7.0以上系统一样了。

1.首先看看沉浸式状态栏的效果
这里写图片描述

步骤1
在BaseActivity的里面加入判断系统版本,方便其他的Activity使用

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initState();
        setContentView(R.layout.activity_base);
    }

    private void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            window.setFlags(                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,                  WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

}

步骤2 MainActivity的xml文件,我在里面写了一个通用的title,请注意title里面的两个属性
android:fitsSystemWindows=”true”
android:clipToPadding=”true”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hyyx.testdemo.MainActivity">

    <include layout="@layout/title_bar" />


    <Button
        android:id="@+id/btn_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转"/>


</LinearLayout>

title的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    android:fitsSystemWindows="true"
    android:clipToPadding="true"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
       >

        <TextView
            android:id="@+id/title_bar_tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="标题"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="18sp" />


    </RelativeLayout>

</LinearLayout>

对了,还需要在AndroidManifest对app的theme进行一个更换

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

经过上面的操作,你的app就可以完成简单沉浸式的效果了。

2.很多人在activty中嵌套fragment时,进行沉浸式的时候标题栏可能会被顶上去,造成沉浸式效果很难看。咱们先看个效果图,是不是遇到过?

这里写图片描述

这里我有一个简单的解决方案,仅供大家参考,就是在放fragment的布局上面,加一个view,不废话看代码。

先看xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hyyx.testdemo.SecActivity">

    <View
        android:id="@+id/statu_bar"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/colorPrimary" />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></FrameLayout>


    <Button
        android:id="@+id/btn_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment-1" />

    <Button
        android:id="@+id/btn_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment-2" />


</LinearLayout>

activit里面的代码

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sec);
          statusBar = findViewById(R.id.statu_bar);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            statusBar.setVisibility(View.VISIBLE);
            setTranslucentStatus(true);

            //还有设置View的高度,因为每个型号的手机状态栏高度都不相同
        } else {
            statusBar.setVisibility(View.GONE);
        }
        fragmentManager = this.getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.frame, new MyFragment());
        transaction.commit();
        ((Button) findViewById(R.id.btn_one)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                statusBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.frame, new MyFragment());
                transaction.commit();
                                                                     }
                                                                 }

        );

        ((Button) findViewById(R.id.btn_two)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                statusBar.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.frame, new MyFragmentTwo());
                transaction.commit();

                                                                     }
                                                                 }

        );

    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

}

这个方法比较投机耍滑,判断SDK版本是否大于等于19,大于就让他显示,小于就要隐藏,然后根据fragment的标题颜色,改变view的背景,实现沉浸式状态栏的效果。我也看过很多资料,很少有人分享解决这个问题的方法,所以各位看官先将就的看看,毕竟也是个解决办法。如果有什么问题,欢迎大家指点和一起谈论,小弟拜谢!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值