【问题标题】:How do I add a shadow beneath my action bar in Android?如何在 Android 的操作栏下方添加阴影?
【发布时间】:2016-02-21 10:43:43
【问题描述】:

我试图让阴影出现在我的操作栏下方,但由于某种原因,阴影不断出现并从屏幕的最顶部开始,而不是在操作栏下方。我在每个 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>

【问题讨论】:

  • 我还应该展示什么?
  • 看不出有什么问题
  • 您可以使用工具栏并对其使用高程,它会给您阴影效果

标签: java android xml


【解决方案1】:

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

【讨论】:

  • 4dp 有什么特别的原因吗?
  • 谢谢你的作品。我想这只是阴影的高度。谢谢。
  • 我通常会添加 4dp,因为我认为它在屏幕上看起来不错,如果我没记错的话,它也是 google 推荐的,但您可以添加任意数量的 DPS。请参阅材料设计指南了解更多详情
【解决方案2】:

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

在应用程序栏中, android:elevation = "1dp"

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

【讨论】:

    猜你喜欢
    • 2014-12-30
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    相关资源
    最近更新 更多