第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

一行代碼使Android狀態(tài)欄變沉浸式透明化

標簽:
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

點擊查看更多內(nèi)容
8人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消