繁体   English   中英

如何在 Android 中的操作栏下方添加阴影?

[英]How do I add a shadow beneath my action bar in Android?

我试图让阴影出现在我的操作栏下方,但由于某种原因,阴影不断出现并从屏幕顶部开始,而不是在操作栏下方。 我在我的每个 xml 布局文件中使用 CustomView android.support.v7.widget.Toolbar操作栏小部件。 我怎样才能让阴影正确出现在这个操作栏下面而不是顶部? 我可以在阴影出现之前将某种分隔线设置为等于此自定义视图的高度吗? 我们可以做一些事情,比如将渐变的高度设置为 50dp,但只显示该高度的最后 5-10% 的渐变吗?

我的主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/white</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#000</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">@color/teal</item>

    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight & colorSwitchThumbNormal. -->

    <item name="android:textColorSecondary">#d7d7d7</item>

    <item name="android:windowContentOverlay">@drawable/actionbar_shadow</item>

</style>

阴影可绘制:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="4dp" />
    <gradient
        android:angle="270"
        android:endColor="@android:color/transparent"
        android:startColor="@color/grey" />
</shape>

工具栏:

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:id="@+id/toolbar"
    android:layout_height="50dp"
    android:minHeight="30dp"
    android:layout_width="match_parent"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    android:background="@color/white">

    <ImageView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/actionbar_title_r"
        android:id="@+id/ActionBarTitle" />

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

仅在运行于API 21或更高版本的设备上,工具栏才会有阴影。 为了达到这种效果,您可以添加属性android:elevation="4dp" ,它将正常工作。 希望这可以帮助

您可以在 material appBar 布局上尝试几个 dp 的高度,看看它提供的效果程度。 然后你决定给它多少。

在 appBar 中,android:elevation = "1dp"

玩 2dp、3dp、4dp 并选择适合您的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM