【问题标题】:How do I add elevation (shadow) to my BottomNavigationView. It doesn't come by default如何将高程(阴影)添加到我的 BottomNavigationView。默认情况下不会出现
【发布时间】:2017-04-07 14:18:10
【问题描述】:

我尝试从 xml 和以编程方式设置海拔高度。但没有任何效果。

【问题讨论】:

  • 你升级到25.0.1了吗?提升是该版本的一个固定问题developer.android.com/topic/libraries/support-library/…
  • 如果你已经升级到 25.0.1 但仍然无法使用可能是因为旧版本的设备不支持它!
  • 我使用的是 25.1.0,但仍然没有看到默认海拔。有什么想法吗?

标签: android material-design bottomnavigationview


【解决方案1】:

只有在 BottomNavigationView 中将白色设置为 android:background 时才有效。

这是我的代码,它正在工作:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/white"
    app:elevation="8dp"
    app:itemIconTint="@color/bottom_color_state"
    app:itemTextColor="@color/bottom_color_state"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.52"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_navigation_main" />

【讨论】:

    【解决方案2】:

    我得到了解决方案。您需要添加背景属性以及高程,否则它不会显示阴影。

    【讨论】:

    • 发布您的代码以获得适合您的解决方案。谢谢。
    • 请出示您的代码,因为它不适合我。
    【解决方案3】:

    这是唯一对我有用的解决方案:

    val background = bottomNavigation.background
    if (background is MaterialShapeDrawable) {
        background.shadowCompatibilityMode = SHADOW_COMPAT_MODE_ALWAYS
    }
    

    这里建议: https://issuetracker.google.com/issues/124153644#comment2

    【讨论】:

      【解决方案4】:

      对于那些感兴趣的人,我还设法使用背景可绘制对象获得了一些阴影。但是,我必须使用白色,否则它不起作用......

      如果你想像我一样做角度,它会很有用。

      drawable/gnt_rounded_corners_shape.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@android:color/white"/>
      
        <corners android:bottomRightRadius="0dp"
            android:bottomLeftRadius="0dp"
            android:topLeftRadius="16dp"
            android:topRightRadius="16dp" />
      </shape>
      

      layout/main_activity.xml

      里面
          <com.google.android.material.bottomnavigation.BottomNavigationView
              android:id="@+id/navigation"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:background="@drawable/gnt_rounded_corners_shape"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:menu="@menu/bottom_nav_menu"
              android:elevation="8dp"
              />
      

      结果:

      【讨论】:

        【解决方案5】:

        如果您不使用CoordinateLayoutBottomNavigationBar,您可以只使用CardViewMaterialCardView 包装您的导航并将cardElevation 设置为您想要的并将cardCornerRadius 设置为零。

        【讨论】:

          猜你喜欢
          • 2016-12-01
          • 2020-12-04
          • 1970-01-01
          • 1970-01-01
          • 2015-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多