2015-12-07 133 views
13

我創建了一個AppBar佈局這樣CoordinatorLayout和AppBarLayout海拔

<android.support.design.widget.AppBarLayout 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/appbar_layout" 
    android:layout_height="@dimen/app_bar_height" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:elevation="20dp"> 

    <android.support.design.widget.CollapsingToolbarLayout...> 
</android.support.design.widget.AppBarLayout> 

它的工作原理,並施放在的LinearLayout陰影:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include layout="@layout/app_bar_large" /> 
</LinearLayout> 

然而,當我把它放到CoordinatorLayout陰影消失:

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include layout="@layout/app_bar_large" /> 
</android.support.design.widget.CoordinatorLayout> 

我該如何讓appbar再次顯示其陰影?

enter image description here

回答

10

這實際上是CollapsingToolbarLayout一個實現細節,如見於source code

if (Math.abs(verticalOffset) == scrollRange) { 
    // If we have some pinned children, and we're offset to only show those views, 
    // we want to be elevate 
    ViewCompat.setElevation(layout, layout.getTargetElevation()); 
} else { 
    // Otherwise, we're inline with the content 
    ViewCompat.setElevation(layout, 0f); 
} 

CollapsingToolbarLayout是顯示非釘扎元素其去除高程 - 缺省值,它只有當只有固定的孩子可見時纔會升高。

+9

如何解決這個問題 –

+1

http://stackoverflow.com/a/39247130/2158970 – Yuraj

+0

https://stackoverflow.com/a/ 32393698/4542217 –

1

的原因是上面,試試這個解決:

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { 
     @Override 
     public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { 
      //some other code here 
      ViewCompat.setElevation(appBarLayout, The Elevation In Px); 
     } 
    }); 
+0

謝謝,它爲我工作。 :) –