2014-05-25 78 views
15

我試圖使透明狀態欄上的Android 4.4+ 我知道如何使它透明通過定義自己的風格:的Android,如何使一個透明的狀態欄正確

<style name="Theme.MyTheme" parent="Theme.MyTheme.Base"> 
    <item name="android:windowTranslucentStatus">true</item> 
    <item name="android:windowTranslucentNavigation">true</item> </style> 

我發現這裏: https://stackoverflow.com/a/20573595/2633630

的問題是,當狀態欄是透明的,它並沒有操作欄的顏色和背景中的活性匹配看出: transparent status bar error

所以我發現我可以使用Android:fitsSystemWindows =「真」和android:clipToPadding在我的佈局,我發現這裏http://mindofaandroiddev.wordpress.com/2013/12/28/making-the-status-bar-and-navigation-bar-transparent-with-a-listview-on-android-4-4-kitkat/

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/background_darkRed" 
android:fitsSystemWindows="true" 
android:clipToPadding="false"  
tools:context="com.sandak......."> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
.... 

這解決了透明的狀態欄和外觀上的問題=「假」確定..不幸的是,在「導航欄」與系統的按鈕,這是紅色的,而不是透明的,因爲我希望在底部的問題: enter image description here

我嘗試所有可能的變種,以達到工作的結果,但我無法弄清楚如何解決它。我唯一的工作解決方案是將大約60dp填充到根元素。但是這個解決方案很難看,而且在不同的設備上可能看起來不一樣,可能不起作用。

我在Google Play上發現了幾個可以與透明背景一起工作的應用程序,所以我很好奇它們是如何使它工作的。

例如: https://play.google.com/store/apps/details?id=com.trello和其他幾個人

+0

當我將'android:windowTranslucentStatus/Navigation'設置爲true時,背景只是半透明的。你做了什麼不同的事情來獲得你截圖中的黑暗漸變效果? – JMRboosties

回答

12

// UPDATE:
好吧,這是很老的答案。現在你可以使用Material主題來處理這個,這很容易。只要目標API設置爲21+,使用支持庫,如果你需要,創造與材料主題的主題,你可以指定狀態欄的顏色直接

<style name="AppTheme" 
     parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- colorPrimary is used for the default action bar background --> 
     <item name="colorPrimary">@color/color_primary</item> 

     <!-- colorPrimaryDark is used for the status bar --> 
     <item name="colorPrimaryDark">@color/color_primary_dark</item> 

     <!-- colorAccent is used as the default value for colorControlActivated 
      which is used to tint widgets --> 
     <item name="colorAccent">@color/accent_orange</item> 
    </style> 

============== ================================================== =======

OLD答:

好吧,看來我解決我的問題。這不是最好和最清晰的解決方案,但它是可行的解決方案。 如果將來有人會找到更好更清晰的解決方案,請讓我知道,我會更新我的答案/或將您的答案標記爲正確答案。但現在,我沒有比這個小黑客更好的解決方案:

佈局的根元素必須是RelativeLayout。比應該遵循你的主要佈局,如ScrollView或任何其他佈局,沒關係。在結束之前,相對佈局標籤之後的結尾是空的LinearLayout,它已經設置了與您的Action Bar相同的背景以及固定的高度(Status Bar的高度)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.sandak...."> 
<ScrollView 
    android:fitsSystemWindows="true" 
    android:clipToPadding="false" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
.... all your main views etc.. 
</ScrollView> 
<LinearLayout 
     android:id="@+id/statusBarBackgroundLinearLayout" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_height="30dp" 
     android:background="@color/background_darkRed" 
     android:clickable="false" 
     android:focusable="false"> 
    </LinearLayout> 
</RelativeLayout> 

好吧,那麼你必須在代碼中設置相同的線性佈局的高度,在狀態欄有:

private void setStatusBarBackground(View rootView) { 
     setStatusBarLayout(rootView); 
     statusBarHeight = getStatusBarHeight(); 
     setStatusBarLayoutHeight(statusBarHeight); 
    } 

    private void setStatusBarLayout(View rootView){ 
     statusBarBackgroundLinearLayout = (LinearLayout) rootView.findViewById(R.id.statusBarBackgroundLinearLayout); 
     if (isPreKitkatDevice()) { 
      hideStatusBarLayout(); 
     } 
    } 

    private int getStatusBarHeight() { 
     int statusBarHeight = 0; 
     int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
     if (resourceId > 0) { 
      statusBarHeight = getResources().getDimensionPixelSize(resourceId); 
     } 
     return statusBarHeight; 
    } 

    private boolean isPreKitkatDevice(){ 
     return Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT; 
    } 

    private void hideStatusBarLayout(){ 
     statusBarBackgroundLinearLayout.setVisibility(View.INVISIBLE); 
    }   

    private void setStatusBarLayoutHeight(int height){ 
     RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) statusBarBackgroundLinearLayout.getLayoutParams(); 
     layoutParams.height = height; 
    } 

然後只需撥打setStatusBarBackground在onCreate()方法。這是所有不同類型設備的工作解決方案。對於不允許透明背景的預裝KitKat設備,您必須隱藏線性佈局。

+2

'getStatusBarLayout(View v)'沒有返回類型?你可以改變這個名字。 :) – Rajkiran

相關問題