2016-01-25 49 views
1

這是我遇到狀態欄重疊工具欄上的預棒棒糖,但不是棒棒糖/棉花糖

在棒棒糖前期設備我有以下問題

enter image description here

在棒棒糖和問題棉花糖設備一切都顯得精細

enter image description here

我試圖創建打開導航抽屜時顯示半透明狀態欄效果。 在棉花糖和棒棒糖設備上,這工作正常。

這裏是我的代碼

activity_base.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawerLayout" 
    android:fitsSystemWindows="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ToolbarStyle" 
      app:theme="@style/ToolbarStyle" /> 

     <FrameLayout 
      android:id="@+id/activity_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <include layout="@layout/view_navigation_drawer_layout" /> 

</android.support.v4.widget.DrawerLayout> 

view_navigation_drawer_layout.xml

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/nav_header" /> 

     <ListView 
      android:id="@+id/lst_menu_items" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.design.widget.NavigationView> 

請注意,我做,因爲這個問題的答案NavigationView and custom Layout以上

注意兩個佈局有android:fitsSystemWindows="true"

我想,也許它是因爲我有佈局,所以我嘗試複製整個佈局到活動佈局。沒有幫助

我也有我的價值觀-V19目錄這個款式的xml文件(KitKat及以上)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="MaterialDesign"> 
     <item name="android:windowTranslucentStatus">true</item> 
    </style> 
</resources> 

回答

0

我做了一個解決這個問題。然而,我的解決方案需要劫持的佈局在這個博客帖子http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/

這裏說一個BaseActivity是我的代碼

public class BaseActivity extends AppCompatActivity { 


    @Override 
    public void setContentView(int layoutResID) { 
     ViewGroup baseView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_base_with_drawer); 
     FrameLayout activityContainer = (FrameLayout) baseView.findViewById(R.id.activity_content); 
     getLayoutInflater().inflate(layoutResID, activityContainer, true); 
     super.setContentView(layoutResID); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if (useToolbar()) { //Does the activity have a toolbar? 
      toolbar.setFitsSystemWindows(true); //toolbar will stretch underneath the status bar and because it naturally has a semi translucent background, it will assume a darker color of the toolbar which is usually the color primary. 
      setSupportActionBar(toolbar); //Set the toolbar 
      ActionBar actionBar = getSupportActionBar(); 
      if (actionBar != null) { 
       actionBar.setDisplayHomeAsUpEnabled(true); 
      } 
     } else { 
      toolbar.setVisibility(View.GONE); 
      activityContainer.setFitsSystemWindows(true); //No toolbar so we set the container to have the fitsSystemWindow attribute to ensure contents do not overlay the system window such as status bar and navigation bar. 
      TypedValue outValue = new TypedValue(); 
      boolean doesThemeHaveTranslucentWindow = getTheme().resolveAttribute(android.R.attr.windowIsTranslucent, outValue, true); //Check if the current activity theme has translucent window, this includes SearchActivity 
      if (!doesThemeHaveTranslucentWindow) { //If it does not then set the container background to colour primary to have the same effect as the toolbar being there and avoid a white status bar. We do not want to do this for translucent windows otherwise they would not be see through 
       activityContainer.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)); 
      } 
     } 
    } 

這是佈局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ToolbarStyle" 
      app:theme="@style/ToolbarStyle" /> 

     <FrameLayout 
      android:id="@+id/activity_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <include layout="@layout/view_navigation_drawer_layout" /> 

</android.support.v4.widget.DrawerLayout> 

FitsSystemWindows不再設在任何XML佈局中。這是在BaseActivity類的setContentViewMethod中完成的。 如果該活動沒有工具欄,則活動容器將fitsSystemWindow屬性設置爲true,否則應用該工具欄。

如果活動沒有工具欄,則其容器佈局的背景顏色設置爲顏色主要。

爲什麼我這樣做是因爲當您在您的工具欄上設置fitsSystemWindows時,它會伸展到狀態欄下方,並且由於狀態欄自然具有半透明背景,因此它會呈現工具欄的較暗顏色,通常是顏色主要所以我們這樣做的活動容器具有相同的效果。

現在有一個用例,我在我的應用程序中,我有一個透明窗口背景的活動,我檢查。如果不是這樣,則活動容器具有此顏色集。

不知道它是否是最好的解決方案,但它迄今在棉花糖,前棒棒糖和pre-KitKat上的運行狀況也很好:P。希望它能幫助別人。