一行代码使Android状态栏变沉浸式透明化

来源:互联网 发布:海康威视4g网络摄像头 编辑:程序博客网 时间:2024/05/16 23:39

传统方法

今天有一个同学问我,怎么使安卓应用的状态栏透明话,便想起将我这个简单的方法介绍给大家。
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":
<relativelayout android:background="#ff0000" android:layout_height="match_parent" android:layout_width="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" span="" style="color:#FF0000;" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">    android:fitsSystemWindows="true"    android:clipToPadding="true"    android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity"></relativelayout>

同时还需要在主题上加上这个:
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

2 0