Appcompat v21工具栏提升前棒棒糖

Tri*_*den 43 android toolbar shadow android-appcompat android-5.0-lollipop

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

在我的应用程序中,我使用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" />
Run Code Online (Sandbox Code Playgroud)

这是我使用的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>
Run Code Online (Sandbox Code Playgroud)

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

Isl*_*san 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>
Run Code Online (Sandbox Code Playgroud)

  • Bravo :) +1为黑客:)为我工作. (6认同)
  • 只要你使用高程,我认为你不能这样做.当我想这样做时,我根本不使用高程.我在工具栏下添加了一个带有渐变背景的视图,就像阴影一样. (3认同)
  • 很好的工作,谢谢你.但是现在我的卡片视图与父宽度不匹配,它在左侧和右侧留下了一些空间.如何使此卡片视图填满所有屏幕宽度?提前致谢 :) (2认同)
  • 修复。对于那些在棒棒糖前版本中工具栏边距有问题的人,请参考这个已接受的答案:http://stackoverflow.com/questions/27477371/android-elevation-not-showing-a-shadow/27518160#comment51895526_27518160 (2认同)

shi*_*jin 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"/>
Run Code Online (Sandbox Code Playgroud)

可绘制的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>
Run Code Online (Sandbox Code Playgroud)

这将解决前棒棒糖设备中的问题.但是我们不希望在棒棒糖和上面的设备中出现这种阴影,因此可以通过棒棒糖及以上设备实现可视性.

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

完成.


Sto*_*eev 12

您可以使用FrameLayoutwith 添加阴影(高程)foreground="?android:windowContentOverlay".Lollipop前不支持elevation属性.因此,如果您正在使用FrameLayout像片段容器那么只需添加前景属性.


Meg*_*kie 7

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

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

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="?android:windowContentOverlay">
Run Code Online (Sandbox Code Playgroud)

别忘了关闭:

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)


Zie*_*ony 6

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

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

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

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

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

看到:


Lee*_*dev 5

我正在使用这个答案:

<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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)