2014-10-29 51 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' 
相关问题