一个activity有多个fragment,分别设置每个fragment的状态栏颜色。
2016-12-14 10:08
513 查看
今天在做一个项目的时候要实现一个activity中可以左右滑动的界面,类似今日头条的新闻界面。我采用的viewpaget和fragment实现。但是问题来了,我想让每个fragment的状态栏有不同的颜色,或者背景图,找了好多博客没找到,如果是activity那就简单了,直接把状态栏设置成透明,然后在布局中设置颜色就可以了,那顺便就把activity的设置方法讲一下。
1.首先定义一个style
最重要的就是 android:fitsSystemWindows=”true” 这句话了,然后设置你想要的颜色 android:background=”@color/gray” 也可以是 图片android:android:background=”@drawable/…….” 这样就可以。代码只粘贴的头部,其他的不用设置,注意这个LinearLayout 是最外层的父布局。
下 面就来讲一下fragment怎么设置了
首先说一下想法,我也在知乎上看到的一句话,就是设置包含viewpager的activity状态栏设置透明,就是上面说到的activity设置状态栏。然后让每个fragment自己修改状态栏颜色。下面就是步骤了。
还是那两句代码 ①android:fitsSystemWindows=”true”
② android:background=”@drawable/caobin”可以设置颜色和图片。其实就是这么简单avtivity和fragment其实差不多,只是activity就设置一个layout,而fragment每个都要设置,并且包含多个fragment的activity状态栏必须设置透明,原来也是查了好多资料,还有说在代码中设置状态栏颜色的
你们可以去试一下,各种问题,就不想说了。对了还有个问题就是,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
还是把代码贴上吧。
设置一个padding,我想是不是可以在代码中获取状态栏的高,然后设置padding的值与状态栏的高一致。谁有好的方法可以留言。
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> 2.每个fragment不都有自己的布局吗,在自己的布局中设置
<?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 4000 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的值与状态栏的高一致。谁有好的方法可以留言。
相关文章推荐
- 一个Activity中多个Fragment实现沉浸式状态栏
- 一个Activity中多个Fragment实现沉浸式状态栏
- 一个Activity中多个Fragment实现沉浸式状态栏
- 通过一个activity打开多个fragment,每个fragment包含一个List
- 在Activity中或Fragment中任意改变状态栏的颜色
- Android开发从一个activity设置跳转到另一个activity中的一个fragment中的一个viewpager中的某一个页面
- ViewPager 结合Fragment实现一个Activity里包含多个可滑动的标签页,每个标签页可以有独立的布局及响应。
- AppCompatActivity设置全屏/状态栏颜色
- 一个activity中有多个fragment的透明状态栏处理
- 一个Activity中多个Fragment实现沉浸式状态栏的解决方法
- QTabWiget Change Color 改变颜色(每个QWidget都有一个自己的调色板palette,设置它的颜色,然后setAutoFillBackground即可)
- Android给Activity状态栏设置自定义颜色
- 一个牛人写的“设置jtable单元格颜色,以及jtable中实现复选框”的程序,备忘
- 不能修改texture2D返回的颜色值?OpenGL ES fragment shader在 iPad2上的一个问题
- Direct2D (40) : 分别设置 IDWriteTextLayout 中文本的颜色
- 如何将一个Activity设置成窗口的样式?
- 如何在activity中动态的设置TextView上字体的颜色
- 处理2D图像和纹理——创建一张纹理,定义每个像素的颜色,将纹理保存到一个文件
- 一个长文档里,包括封面、不同的章节,如果我想封面不设置页眉页脚,每个章节的页眉都不同,请问应该如何设置页眉页脚?
- 如何设置一个弹出的activity