【问题标题】:Android: I cannot remove elevation / shadow on my toolbarAndroid:我无法删除工具栏上的高程/阴影
【发布时间】:2023-03-27 11:13:01
【问题描述】:

您好,我想从 API 21 及更高版本的工具栏中删除高程和阴影效果。以下是我尝试过的

setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setElevation(0);

我的 XML 工具栏。我试图将高度设置为 0dp

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:elevation="0dp"
    android:theme="@style/ToolbarStyle"
    app:theme="@style/ToolbarStyle"
    >

如果有帮助,这就是 ToolbarStyle

<style name="ToolbarStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="colorControlNormal">@android:color/white</item>
</style>

编辑 1:在样式 v21 中尝试了以下内容。结果还是一样

<style name="ToolbarStyle" parent="ThemeOverlay.AppCompat.Dark">
    <item name="colorControlNormal">@android:color/white</item>
    <item name="android:elevation">0dp</item>
</style>

编辑 2:工具栏在我的布局中的位置

android.support.v4.widget.DrawerLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <include layout="@layout/toolbar" />

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以通过一个简单的步骤来做到这一点。不要从布局中删除 android.support.design.widget.AppBarLayout,而是将属性 app:elevation="0dp" 添加到其中。您的最终布局将是:

    <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:elevation="0dp">
                ...
    </android.support.design.widget.AppBarLayout>
    

    【讨论】:

    • 当我这样做时,工具栏会完全消失。
    • 请告诉我您的布局层次结构。这样我可以为您提供帮助。
    • 不知道你是如何修复它的,但修复它的一种方法是使用findViewById(R.id.appBarLayout).bringToFront();
    • 有趣的是 android:elevation="0dp" 没有做到这一点(在 SDK 21+ 设备上)
    • android:elevation 不起作用。为了获得最佳结果,请使用 android:stateListAnimator="@null"
    【解决方案2】:

    这会移除工具栏上的高度(android:stateListAnimator="@null"),stateListAnimator 适用于 API 级别 21 或更高级别,因此插入一个 tools 属性(在命名空间声明之前),如下所示:

    //xmlns:tools="http://schemas.android.com/tools" in parent or start element
    
         <android.support.design.widget.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stateListAnimator="@null" 
            android:theme="@style/AppTheme.AppBarOverlay"
            tools:targetApi="21">
    
     <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:elevation="0dp"
                    app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
    //your content goes here like RecyclerView, Card or other layout 
    

    这是我的示例结果:

    【讨论】:

    • 我喜欢这种布局。
    【解决方案3】:

    在您的 appBar 上使用 setOutlineProvider(null);。请务必检查 SDK 版本。 :)

    【讨论】:

      【解决方案4】:

      在您的 AppBarLayout 中使用 app:elevation="0dp"。确保像这样将 Toolbar 放在 AppBarLayout 中 -

          <android.support.design.widget.AppBarLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="?attr/actionBarSize"
              android:theme="@style/AppTheme.AppBarOverlay"
              android:fitsSystemWindows="true"
              app:elevation="0dp"> 
      
              <android.support.v7.widget.Toolbar
                  xmlns:app="http://schemas.android.com/apk/res-auto"
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@color/firstOption"
                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
      
          </android.support.design.widget.AppBarLayout>
      

      并将这一行添加到您的活动中 - findViewById(R.id.appBarLayout).bringToFront();
      这会将 AppBar 带到前面。

      【讨论】:

        【解决方案5】:

        只需删除布局层次结构的这一部分:

        <android.support.design.widget.AppBarLayout
                        android:id="@+id/appBarLayout"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
        

        它负责创建阴影。工具栏本身不会投射任何阴影。

        【讨论】:

        • 如果你添加elevation=0dp到AppBarLayout,它就会消失。
        【解决方案6】:

        转到要删除 ActionBar 的 Elevation 的活动。 在 setContent(....) 之前,请求 ActionBar 功能(即如果你没有直接声明)

            getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
            getSupportActionBar().setElevation(0);
        

        或者只调用声明的 Toolbar 变量 例如 工具栏.setElevation(0);

        【讨论】:

          【解决方案7】:

          在代码中获取appbarlayout:

          var appbarLayout = findviewbyid<AppBarLayout>(resource.id.  );  
          

          设置属性

          appbarLayout.StateListAnimator=null;
          

          【讨论】:

          • 欢迎来到 Stack Overflow。请查看答案窗口右上角的“帮助”以了解如何格式化代码。
          • 谢谢你 Martin Evans。我会按照你指定的方式进行操作
          【解决方案8】:

          为 v21 创建另一个样式并添加

          &lt;item name="android:elevation"&gt;@dimen/toolbar_elevation&lt;/item&gt; 到那种风格。在普通样式上不要使用海拔,也不要在 xml 中使用,只需使用style="@style/yourCustomToolbarStyle"

          【讨论】:

          • 您好,感谢您的回复。我进行了更改,但问题仍然存在。请查看我的代码编辑
          • 这是我的自定义工具栏的 xml:&lt;android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" style="@style/ToolBarStyle" android:layout_width="match_parent" android:layout_height="@dimen/abc_action_bar_default_height_material" app:contentInsetLeft="0dp" app:contentInsetStart="0dp" android:background="@color/light_gray"&gt; style-v21 在哪里我有 4dp 的高程,而普通样式没有高程
          • 所以我应该在 ToolbarStyle 的 v21 样式版本上设置 4dp 高度?
          • 可能问题出在您的工具栏父级上。我有&lt;style name="MyName" parent=""&gt;,我使用&lt;item name="popupTheme"&gt;&lt;item name="theme"&gt;
          • 是的,它可能是,我将高度设置为 1000dp :P 并删除 getSupportActionBar().setElevation(o) 行。海拔保持不变。我将在工具栏是子项的地方发布父项
          【解决方案9】:

          解决方案很简单。只需将以下行添加到您的 AppBar 布局中。此后无需为 Toolbar 或 AppBar 布局添加高程。

          android:stateListAnimator="@null"
          

          【讨论】:

            【解决方案10】:

            使用 android:outlineSpotShadowColor="assign transparent colour" 你的阴影将消失,我的工作如下

            enter image description here

            【讨论】:

              【解决方案11】:

              要使用 java 代码删除海拔,请使用下面的行...

              getSupportActionBar().setElevation(0);
              

              【讨论】:

              • 这条线必须在哪里实现?请进一步解释。
              • 就在 setContentView(R.layout.activity_main) 的下方; onCreate() 方法。
              猜你喜欢
              • 2018-01-03
              • 1970-01-01
              • 2021-02-13
              • 2023-04-01
              • 2019-08-15
              • 1970-01-01
              • 1970-01-01
              • 2016-04-30
              • 1970-01-01
              相关资源
              最近更新 更多