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

传统方法

	今天有 bug,app的状态栏透明,便想起将简单的方法。

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 
		xmlns:android="http://schemas.android.com/apk/res/android" 
 		xmlns:tools="http://schemas.android.com/tools"  
		android:background="#ff0000" 
		android:layout_height="match_parent"
 		android:layout_width="match_parent" 
    	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:

ViewActivity.setStatusBarColor(this,getResources().getColor(android.R.color.holo_blue_light));

注意一定要设置在setContentView()方法之后:

setContentView(R.layout.layout);  //它是后面
ViewActivity.setStatusBarColor(this, getResources().getColor(R.color.blue));  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值