2015-12-07 92 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

谢谢,它为我工作。 :) –