Appcompat v21工具栏提升前棒棒糖

时间:2014-11-04 19:24:02

标签: android toolbar shadow android-5.0-lollipop android-appcompat

首先,我知道之前已经问过这个问题,但之前没有得到回答。我希望有人能给我一个答案。

在我的应用程序中,我使用Appcompat_v7(API 21)中的工具栏。这是我的代码:

<android.support.v7.widget.Toolbar
    style="@style/DarkActionbarStyle"
    android:id="@+id/toolBar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/actionbar_height" />

这是我使用的ToolBar样式:

<style name="DarkActionbarStyle" parent="@style/Widget.AppCompat.Toolbar">
    <item name="android:background">?attr/colorPrimary</item>
    <item name="titleTextAppearance">@style/ActionBarTitle</item>
    <item name="android:elevation">2dp</item>
    <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
    <item name="theme">@style/ThemeActionBarDark</item>
</style>

<style name="ThemeActionBarDark" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="actionBarItemBackground">@drawable/btn_dark_orange</item>
    <item name="selectableItemBackground">@drawable/btn_dark_orange</item>
</style>

问题是,提升前棒棒糖不起作用。 所以我的问题是:棒棒糖设备上的ToolBar下是否有阴影?

10 个答案:

答案 0 :(得分:75)

这对我很有用:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary"
    card_view:cardElevation="4dp"
    card_view:cardCornerRadius="0dp">

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

</android.support.v7.widget.CardView>

答案 1 :(得分:16)

将CardView容器用于工具栏是一个坏主意。

CardView很重,特别是对于低端设备。

最好的方法是在工具栏下方放置渐变阴影视图。阴影视图必须是协调器布局的直接子视图。即。包含工具栏和阴影视图的appbar必须是兄弟姐妹。

将此视图组件添加到布局中。

 <View
    android:id="@+id/gradientShadow"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:background="@drawable/toolbar_shadow"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_collapseMode="pin"/>

drawable toolbar_shadow.xml

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

这将解决前棒棒糖设备中的问题。但是我们不希望在棒棒糖及以上设备上出现这种阴影,因此可以看到棒棒糖及以上设备的可见性。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        findViewById(R.id.gradientShadow).setVisibility(View.GONE);
}

完成。

答案 2 :(得分:12)

您可以使用FrameLayout foreground="?android:windowContentOverlay"添加阴影(海拔)。 Lollipop前不支持elevation属性。因此,如果您使用FrameLayout之类的片段容器,只需添加前景属性即可。

答案 3 :(得分:7)

由于我遇到了CardView小部件方法的问题,我使用了@Sniper提到的FrameLayout方法;它工作得很好!

我只想分享您必须使用的代码段。 只需将其直接放在主要内容开始的工具栏下:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:windowContentOverlay">

不要忘记关闭:

</FrameLayout>

答案 4 :(得分:6)

可以拥有真实的阴影 - 动画和生成。 Lollipop使用的方法自Froyo开始提供。我认为Honeycomb用于阴影生成的硬件加速可用。以下是它的工作原理:

  • 将您的视图绘制到一个将LightingColorFilter设置为0,0
  • 的离屏位图
  • 使用ScriptIntrinsicBlur类和高程值作为半径模糊黑色形状(离屏位图)
  • 在视图下方绘制位图

它需要添加自定义高程属性,能够渲染阴影的自定义视图,以及使用渲染脚本和兼容性库(对于旧设备)。我不打算深入研究细节,因为其中很多都包括编译和次要性能优化问题。但这是可能的。

为什么官方支持库中没有阴影?

  • 它需要更改UI框架,因为它无法自由绘制外部视图边界
  • 平滑动画需要相当不错的GPU

请参阅:

答案 5 :(得分:5)

我正在使用此answer

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

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/orange"
    android:titleTextAppearance="@color/White"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

<强> toolbar_shadow.xml

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

答案 6 :(得分:4)

无法在API 21(Android Lollipop)之前使用提升属性。 您可以然后以编程方式添加阴影,例如使用位于Toolbar下方的自定义视图。

例如:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="@drawable/shadow"/>

阴影是可绘制的黑色渐变。

答案 7 :(得分:2)

要在工具栏下显示阴影,请使用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>

要使用Google Android设计支持库,请在build.gradle文件中输入以下内容:

 compile 'com.android.support:design:22.2.0'

答案 8 :(得分:1)

只要没有操作栏菜单,手动添加阴影的解决方案就可以正常工作。如果是这样,阴影视图将在操作栏图标之前停止。

我认为顶部有一个垂直线性布局,顶部是appbar,另一个是下一个线性布局项目,或者在我的情况下,它是

<LinearLayout Vertical> 
  <v7 toolbar/>
  <RelativeLayout>
     <View for shadow with alignParent_top= true/>
      ....
  </RelativeLayout>
</LinearLayout>

我真的希望不久的将来appCompat会解决这个问题。

答案 9 :(得分:0)

另一个选项是以下库提供9patch阴影,例如iosched app,android-materialshadowninepatch

相关问题