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

标签 java android xml

我试图让阴影出现在我的操作栏下方,但由于某种原因,阴影不断出现并从屏幕的最顶部开始,而不是在操作栏下方。我在我的每个 xml 布局文件中使用 CustomView android.support.v7.widget.Toolbar 操作栏小部件。我怎样才能让阴影正确出现在这个操作栏下面而不是顶部?我可以在阴影出现之前将某种分隔线设置为等于此自定义 View 的高度吗?我们可以做一些事情,比如将渐变的高度设置为 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" 它会正常工作。希望这有帮助

关于java - 如何在 Android 中的操作栏下方添加阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35535092/

相关文章:

java - Android:如何避免点击通知调用 onCreate()

java - 如何处理冒号(:) in Attribute in JAXB?

java多服务器聊天架构

java - 搜索单词时,logstash 抛出 org.elasticsearch.action.search.SearchPhaseExecutionException

java - 如何定义一个好的存储库接口(interface)

Android在哪里存储可编辑文件

c# - 设置 Xamarin.Forms - "No resource found that matches the given name..."

android - 如果点击 FAB 菜单以外的任何地方,如何关闭 float 操作按钮菜单?

python - 如何使用 Python 将此 XML 字符串转换为二进制形式?

sql - 如何从sql中的ntext(xml格式)列中获取值