安卓设置状态栏颜色

来源:互联网 发布:红颜知已歌曲人名 编辑:程序博客网 时间:2024/05/14 01:06

如何在4.4以上的系统中方便的设置状态栏颜色,下面就是实现的步骤。
使用Eclipse,下载JAR包,并引入到项目的libs文件夹中。

需要的方法:

private void applyKitKatTranslucency() {    

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

        setTranslucentStatus(true);    
        SystemBarTintManager mTintManager = new SystemBarTintManager(this);                                   mTintManager.setStatusBarTintEnabled(true);       
        mTintManager.setStatusBarTintResource(R.color.colorTop);//所需颜色 
  }  
}
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);  
}
然后, 在OnCreate()方法中调用applyKitKatTranslucency方法
在style.xml中,添加系统的样式:
<!-- 去掉tab顶部的黑边 -->  
  <style name="no_title" parent="@android:style/Theme.Light.NoTitleBar">        
  <!-- 沉浸式状态栏 -->  
   <item name="android:fitsSystemWindows">true</item>  
   <item name="android:clipToPadding">false</item>  
 </style>  
在AndroidManifest.xml进行配置主题:(如果不添加,会造成一些页面的变形。)
android:theme="@style/no_title"


0 0
原创粉丝点击