Android StatusBar


獲取狀態欄高度


/**
 * 獲取狀態欄高度
 * @return
 * @hide
 */
public static int getStatusBarHeight() {
    return Resources.getSystem().getDimensionPixelSize(Resources.getSystem().getIdentifier("status_bar_height", "dimen", "android"));
}

/**
 * 獲取狀態欄高度
 *
 * @param context
 * @return
 */
public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;
    Field field = null;
    int x = 0, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}


Android 5.0 全透明 通知欄

//狀態欄完全透明(大部分機型只設置FLAG_TRANSLUCENT_STATUS的時候會半透明)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);
}


Android 5.0 通知欄  狀態欄 顏色改變

1、通過設置Style 設置

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:statusBarColor">@color/red</item>
</style>

2、通過代碼設置

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    // 標記可改變 SYSTEM_BAR 顏色,加了這一行纔可以修改 狀態欄顏色
    // 如果不加這行則 需要在相應的activity 的 Theme 屬性中標記
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(getResources().getColor(R.color.background_dark));
}

3、適配時 的注意點

 使用Theme style 設置時,有適配問題,在5.0以下,是不可以改變狀態欄的, 
     所以需要將建立一個   values-v21 文件夾, 在裏面放着相應的style文件  
     同時以下只能在5.0 使用的 相關配置,都應只放在  values-v21文件夾下

     v21 表示 Android 5.0 






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章