为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
Android
传统方法

今天有一个同学问我,怎么使安卓应用的状态栏透明话,便想起将我这个简单的方法介绍给大家。
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

点击查看更多内容
8人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消