android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)

这个特性是andorid4.4支持的,最少要api19才可以使用。下面介绍一下使用的方法,非常得简单:

[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.   
  8.         //透明状态栏  
  9.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  10.         //透明导航栏  
  11.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  
  12.   
  13.     }  
  14.   
  15.   
  16. }  


[java]  view plain copy
  1. //透明状态栏  
  2. getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
  3. //透明导航栏  
  4. getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);  

只要加入这两行代码,就可以实现沉浸式通知栏了。效果如图:



给大家看看这个界面的布局:

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#ffffff"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity">  
  8.   
  9.   
  10.     <TextView  
  11.   
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="100dp"  
  14.         android:background="#009959" />  
  15.   
  16.   
  17.     <Button  
  18.         android:layout_width="100dp"  
  19.         android:layout_height="50dp"  
  20.         android:background="#ff669d"/>  
  21.   
  22. </LinearLayout>  

是一个垂直的流布局,但这样,其实还是有问题的,我在textView里面加一些文字,就是绿色的那一块,大家看一下效果:


大家看到了吧,文字和状态栏重叠在一起了,这肯定是不行的,此时需要添加下面的代码:


[html]  view plain copy
  1. android:fitsSystemWindows="true"  
  2. android:clipToPadding="true"  

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.       
  6.     android:fitsSystemWindows="true"  
  7.     android:clipToPadding="true"  
  8.   
  9.     android:background="#ffffff"  
  10.     android:orientation="vertical"  
  11.     tools:context=".MainActivity">  
  12.   
  13.   
  14.   
  15.     <TextView  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="100dp"  
  18.         android:background="#009959" />  
  19.   
  20.   
  21.     <Button  
  22.         android:layout_width="100dp"  
  23.         android:layout_height="50dp"  
  24.         android:background="#ff669d"/>  
  25.   
  26. </LinearLayout>  

大家看红色的那部分,加入那两行以后,界面仍然会是沉浸式的,但状态栏那部分,就不会再重叠了,像加了padding一样,如下图:


大家看图,绿色的textView和红色的一个button都被下移了,状态栏是白色的,是背景linearLayout的颜色。很明显,这也不是我们想要的,我们希望状态栏和我们放在顶部的控件是同一个颜色,同时,控件内容也不和状态栏重复,其实,只要把那两行代码放到我们顶部的控件就可以了。代码如下:


[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#ffffff"  
  6.     android:orientation="vertical"  
  7.     tools:context=".MainActivity">  
  8.   
  9.   
  10.   
  11.     <TextView  
  12.         android:fitsSystemWindows="true"  
  13.         android:clipToPadding="true"  
  14.   
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="100dp"  
  17.         android:background="#009959"  
  18.         android:text="你好,请问你有男朋友吗"/>  
  19.   
  20.   
  21.     <Button  
  22.         android:layout_width="100dp"  
  23.         android:layout_height="50dp"  
  24.         android:background="#ff669d"/>  
  25.   
  26. </LinearLayout>  
就是那两行红色的代码,放在绿色的textView上,这样,就会是下面的效果:

这就是我们想要的了。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值