2014-10-29 50 views
14

如您所知,Elevation在Pre-Lollipop設備上不起作用。因此,appcompat-v7中的默認應用程序欄使用「僞陰影」紋理(我喜歡稱之爲)來模擬陰影。我的問題是我需要使用自定義工具欄。當我使用自定義工具欄時,該「僞陰影」不存在。所以它看起來很平坦。任何想法如何添加陰影回來?有些人在其他論壇上說過要添加一個FrameLayout,前景是「android:windowContentOverlay」,它與ToolBar重疊。可悲的是,我還沒有找到任何可行的辦法。由於某種原因,在我的測試中,「android:windowContentOverlay」無論如何都是不可見的。不知道我做錯了什麼。 :/使用工具欄時陰影不起作用(棒棒糖appcompat-v7)

下面是我的工具欄上的佈局XML數據:

<android.support.v7.widget.Toolbar 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

下面是它看起來像使用默認程序兼容性AppBar:http://imgur.com/0EiE1Vv

下面是它看起來像一個自定義工具欄:http://imgur.com/GGEC6Tq

編輯:在alanv的幫助下,我想出瞭如何在工具欄下創建陰影。但是,它與AppCompat中默認的不同。這只是一個微弱的陰影,如果我沒有記錯的話,它就是在舊版本中使用過的相同的陰影資源。我很難找到默認AppCompat欄的資源。

+1

前景應該用設置機器人:前景= 「機器人:ATTR/windowContentOverlay」。FrameLayout應該位於您的工具欄下方,並且應該包含您的應用內容。 – alanv 2014-10-29 22:03:15

+1

感謝您的提示。它實際上*創造了一個影子,雖然它很微弱。不幸的是,我不太在尋找什麼。也許有另一個資源比windowContentOverlay這樣做?我似乎無法找到任何東西。 – Michael 2014-10-30 04:20:13

+0

@Phascinate是否爲這兩個疊加層,工具欄和下面的內容找到適當的資源?它看起來像是兩個不同的疊加層,我正在尋找資源來實現類似於股票工具欄的結果。 – tomrozb 2014-12-01 13:51:38

回答

0

我只想有一個自定義XML的影子,在你的繪製文件夾中的ImageView

<ImageView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/shadow" 
android:layout_height="@dimen/shadow_height" 
android:layout_width="match_parent" 
android:src="@drawable/shadow_shape" 
    /> 

其圖像資源shadow_shape.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<gradient android:startColor="@color/shadow" 
    android:endColor="@color/transparent" 
    android:angle="-90" /> 
</shape> 

你可以通過相當不錯的結果根據您的需要調整@dimen/shadow_height@color/shadow(黑暗的@color/shadow &視圖越高,虛假仰角越高)。這不是本機的,但非常可定製。

1

你會把它放在工具欄下。

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <View 
     android:layout_width="match_parent" 
     android:layout_height="5dp" 
     android:background="@drawable/toolbar_shadow" /> 
</FrameLayout> 

@繪製/ toolbar_shadow:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
<gradient 
    android:startColor="@android:color/transparent" 
    android:endColor="#88333333" 
    android:angle="90"/> 
</shape> 
1

我會建議你使用使用兩個不同的佈局(一個21版本,一個針對所有其他人),包括他們在您的佈局:

<include layout="@layout/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

例子:https://gist.github.com/P0T4T0x/fbd2151bb40d3bd635d0

1

要顯示你的toolb下的陰影請使用Google Android設計支持庫中的AppBarLayout。這是一個如何使用它的例子。

<android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
    <android.support.v7.widget.Toolbar 
      android:layout_height="?attr/actionBarSize" 
      android:layout_width="match_parent"/> 
    </android.support.design.widget.AppBarLayout> 

要使用谷歌Android設計支持庫輸入以下到您的build.gradle文件:

compile 'com.android.support:design:22.2.0' 
相關問題