传统方法

今天有一个同学问我,怎么使安卓应用的状态栏透明话,便想起将我这个简单的方法介绍给大家。 Google 在Android 4.4时给全屏阅读文字或玩游戏这种情景增加了透明状态栏和透明导航栏的功能,网上大多数Blog都有介绍如何透明化状态栏,有点过于麻烦,需要修改XML和Activity代码,无非都是类似下面这种通用的方法,Android状态栏透明化需要在Activity中添加如下代码:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //透明状态栏    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);    //透明导航栏   getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);}

还需要在Xml中加android:fitsSystemWindows="true",和android:clipToPadding="true":
    android:fitsSystemWindows="true"    android:clipToPadding="true"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">

同时还需要在主题上加上这个:
android:theme="@android:style/Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor"            android:theme="@android:style/Theme.Holo.Light.NoActionBar.TranslucentDecor"            android:theme="@android:style/Theme.Holo.NoActionBar.TranslucentDecor"

一行代码

下面介绍下我整合过后,并一直沿用的方法,先在工具类中添加如下静态方法:
    @TargetApi(19)    public static void setStatusBarColor(Activity activity, int color) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            ViewGroup decorViewGroup = (ViewGroup)activity.getWindow().getDecorView();            //获取自己布局的根视图            View rootView = ((ViewGroup) (decorViewGroup.findViewById(android.R.id.content))).getChildAt(0);            //预留状态栏位置            rootView.setFitsSystemWindows(true);            //添加状态栏高度的视图布局,并填充颜色            View statusBarTintView = new View(activity);            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,                    PhoneInfo.getInternalDimensionSize(activity.getResources(), "status_bar_height"));            params.gravity = Gravity.TOP;            statusBarTintView.setLayoutParams(params);            statusBarTintView.setBackgroundColor(color);            decorViewGroup.addView(statusBarTintView);        }    }    public static int getInternalDimensionSize(Resources res, String key) {        int result = 0;        int resourceId = res.getIdentifier(key, "dimen", "android");        if (resourceId > 0) {            result = res.getDimensionPixelSize(resourceId);        }        return result;    }

以后就只需要在Activity中添加这一行代码,不用修改其他地方,第一个参数为Activity,第二个为颜色Id
PhoneInfo.setStatusBarColor(this, getResources().getColor(android.R.color.holo_blue_light));

注意一定要设置在setContentView()方法之后,如:
setContentView(R.layout.layout);PhoneInfo.setStatusBarColor(this, getResources().getColor(R.color.blue));


声明

欢迎转载,但请保留文章原始出处
作者:Jaiky_杰哥 
出处:http://blog.csdn.net/jaikydota163/article/details/53012777

更多相关文章

  1. Android上访问Java做的WebService获取JSON数据的方法及org.xmlpu
  2. Android(安卓)Launcher研究(一)-----------图文详解手把手教你在
  3. Android(安卓)NDK开发之入门示例 用C++ 写一个能四则运算的计算
  4. Android(安卓)Developers:指定在线程中运行的代码
  5. Android中的动态加载机制
  6. Android通过scroller实现缓慢移动
  7. Android(安卓)Frame动画概述及示例
  8. android桌面添加快捷方式之判断是否存在无效
  9. Android之BaseAdapter的优雅实现

随机推荐

  1. android scaleType的属性
  2. android里发送mail的几种方式
  3. Linux 下Android 开发环境搭建
  4. (转载自diycode)2017 Android 面试题分享
  5. Maven开发Android指南 2 配置android-mav
  6. 安装|卸载apk文件在Android仿真器中
  7. android 画虚线、实线,画圆角矩形,一半圆角
  8. Android 侧滑菜单的实现
  9. Android NFC架构分析
  10. android EditText设置文字选中