一个activity有多个fragment,分别设置每个fragment的状态栏颜色。

今天在做一个项目的时候要实现一个activity中可以左右滑动的界面,类似今日头条的新闻界面。我采用的viewpaget和fragment实现。但是问题来了,我想让每个fragment的状态栏有不同的颜色,或者背景图,找了好多博客没找到,如果是activity那就简单了,直接把状态栏设置成透明,然后在布局中设置颜色就可以了,那顺便就把activity的设置方法讲一下。
1.首先定义一个style

 <style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>
        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

2.在manifests中设置activity的theme

  <activity
            android:name=".ui.NewsDetailActivity"
            android:theme="@style/ColorTranslucentTheme"></activity>

3.然后就是在布局文件中设置了

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

最重要的就是 android:fitsSystemWindows=”true” 这句话了,然后设置你想要的颜色 android:background=”@color/gray” 也可以是 图片android:android:background=”@drawable/…….” 这样就可以。代码只粘贴的头部,其他的不用设置,注意这个LinearLayout 是最外层的父布局。

下 面就来讲一下fragment怎么设置了
首先说一下想法,我也在知乎上看到的一句话,就是设置包含viewpager的activity状态栏设置透明,就是上面说到的activity设置状态栏。然后让每个fragment自己修改状态栏颜色。下面就是步骤了。

 1.就是让activity设置透明了状态栏,只要设置activity的theme就可以了,布局不需要改,这句话就是设置状态栏为透明name="android:statusBarColor">@android:color/transparent</item>

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@drawable/caobin"
    android:orientation="vertical">

还是那两句代码 ①android:fitsSystemWindows=”true”
② android:background=”@drawable/caobin”可以设置颜色和图片。其实就是这么简单avtivity和fragment其实差不多,只是activity就设置一个layout,而fragment每个都要设置,并且包含多个fragment的activity状态栏必须设置透明,原来也是查了好多资料,还有说在代码中设置状态栏颜色的

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    
    //因为不是所有的系统都可以设置颜色的,在4.4以下就不可以。。有的说4.1,所以在设置的时候要检查一下系统版本是否是4.1以上  
            Window window = getWindow();  
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);  
            window.setStatusBarColor(getResources().getColor(R.color.action_color_blue));  
        }  

你们可以去试一下,各种问题,就不想说了。对了还有个问题就是,4.4之后加入windowTranslucentStatus的属性,所以想用必须是4.4之后的版本,之前的版本我就不知道了,我也是小白,还是就是4.4和5.0有差别,具体我也不知道。那个style要写在values-19下面。我说的这个方法在fragment中在嵌套fragment也可以用。好了差不多说完了,关于版本问题你们可以看看鸿洋的博客http://blog.csdn.net/lmj623565791/article/details/48649563/

今天突然发现上面说的fragment设置方法会导致错位,就是控件会占据状态栏的位置

要用的话,只能是最上要设置padding。 android:layout_paddingTop=”20dp”这个DP值自己把握。鸿神的博客也有提到!每个手机的状态栏高度都不一样,大多数都是20dp,要适配手机的话不建议直接设置paddingTop=20dp,github上面有个statusBarUtils项目,里面的做法是获取状态栏的高度然后加一个透明的且与状态栏一样高的view站着地方,有需要的可以去看看。https://github.com/laobie/StatusBarUtil

想要笔者的demo的点这里:https://github.com/caobin821651400/news2
还是把代码贴上吧。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/gray"
    android:fitsSystemWindows="true"
    android:paddingTop="20dp"
    android:orientation="vertical">

    <TextView
        android:gravity="center"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="关心界面" />

</LinearLayout>

设置一个padding,我想是不是可以在代码中获取状态栏的高,然后设置padding的值与状态栏的高一致。谁有好的方法可以留言。

Guess you like

Origin blog.csdn.net/weixin_37438128/article/details/109226834