【问题标题】:Transparent AppBarLayout透明的 AppBarLayout
【发布时间】:2019-08-18 20:04:31
【问题描述】:

我正在开发一个应用程序,我希望有一个透明的工具栏 (AppBarLayout),但仍然可以看到导航图标按钮。不幸的是,我能做到的最好的就是透明的工具栏,它仍然会留下阴影(高度)。

我有一个全屏对话框片段,请看样式:

<style name="FullScreenImageStyle" parent="AppTheme">
        <item name="android:windowNoTitle">true</item>

        <item name="android:windowFullscreen">false</item>
        <item name="android:windowIsFloating">false</item>


        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_down</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
    </style>

片段的布局看起来像这样(这里只是开始):

<androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent">

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

    </com.google.android.material.appbar.AppBarLayout>

稍后在初始化对话框片段时,我设置了全屏样式和导航图标的资源ID。

结果如下所示。请注意屏幕顶部的工具栏阴影:

请问有什么办法解决这个问题吗?

【问题讨论】:

  • 只需在“AppBarLayout”中使用 app:elevation="0dp" 即可去除阴影。
  • @vishal 已经试过了。整个导航图标消失。
  • 完全不要使用应用栏布局。使用全屏主题并制作自己的布局。
  • 使用直接工具栏。删除 AppBarLayout。
  • 尝试仅使用工具栏(会有意义),但导航图标消失。如果我添加translationZ="2dp",它会起作用,但它很hacky并且仍然不能回答原始问题。

标签: android material-design androidx


【解决方案1】:

去掉AppBarLayout,直接使用Toolbar

<androidx.coordinatorlayout.widget.CoordinatorLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <androidx.appcompat.widget.Toolbar
     android:id="@+id/toolbar"
     android:layout_width="match_parent"
     android:layout_height="?attr/actionBarSize"
     android:background="@android:color/transparent"
     android:fitsSystemWindows="true"
     android:paddingStart="6dp"
     android:paddingEnd="16dp"
     android:translationZ="1dp"
     app:contentInsetStartWithNavigation="0dp"
     app:navigationIcon="@android:drawable/ic_menu_close_clear_cancel" />

  <FrameLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">

     //your content

  </FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

它会给你这个视图:

【讨论】:

    【解决方案2】:

    这样做

     <android.support.design.widget.AppBarLayout
      ...
      android:stateListAnimator="@null"
      ... />
    

    【讨论】:

      【解决方案3】:

      使用此代码

       <com.google.android.material.appbar.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@null">
      
      appBarLayout.setOutlineProvider(null)
      

      【讨论】:

        【解决方案4】:

        最后我通过删除AppBarLayout,将工具栏设置为透明背景并添加translationZ解决了这个问题,觉得有点hacky。

        下面是代码供参考:

        <androidx.coordinatorlayout.widget.CoordinatorLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true">
        
            <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:translationZ="2dp"
                    android:fitsSystemWindows="true"/>
        
            <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
        
        
                <!-- Some other layouts -->
        
            </androidx.constraintlayout.widget.ConstraintLayout>
        
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        

        【讨论】:

          【解决方案5】:

          试试这个代码 .. 并输入您的图像和您的需求。此代码在工具栏图标和一些文本和右侧也搜索图标..

            <com.google.android.material.appbar.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              >
          
              <androidx.appcompat.widget.Toolbar
                  android:id="@+id/toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="?attr/actionBarSize"
                  android:background="@color/white"
                  android:theme="@style/MyToolbar"
                  app:contentInsetStart="0dp"
                  app:contentInsetStartWithNavigation="0dp"
                  app:titleTextAppearance="@style/MyToolbar">
          
                  <ImageView
                      android:id="@+id/headerIcon"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="@dimen/_6sdp"
                      android:src="@drawable/ic_edit_location_black"
                      android:visibility="gone" />
          
                  <androidx.constraintlayout.widget.ConstraintLayout
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginStart="@dimen/_6sdp">
          
                      <TextView
                          android:id="@+id/header"
                          style="@style/MyToolbar"
                          android:layout_width="wrap_content"
                          android:layout_height="?attr/actionBarSize"
                          android:gravity="center_vertical"
                          android:textColor="@color/primary"
                          app:layout_constraintStart_toStartOf="parent" />
          
                      <ImageView
                          android:id="@+id/ivSearch"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_marginEnd="@dimen/_10sdp"
                          android:src="@drawable/search_icon"
                          android:visibility="gone"
                          app:layout_constraintBottom_toBottomOf="parent"
                          app:layout_constraintRight_toRightOf="parent"
                          app:layout_constraintTop_toTopOf="parent" />
          
                  </androidx.constraintlayout.widget.ConstraintLayout>
          
              </androidx.appcompat.widget.Toolbar>
          
          
          </com.google.android.material.appbar.AppBarLayout>
          

          【讨论】:

            【解决方案6】:

            试试这个;

             android:background="@color/transparent"
             android:elevation="0dp"
             app:elevation="0dp"
            
            <color name="transparent">#00000000</color>
            

            【讨论】:

            • 它甚至导致导航图标不可见。
            • @JakubGruber 。您的 xml 外观添加到大多数细节
            • 我通过添加translationZ="2dp" 解决了这个问题,但这有点hacky。认为您的解决方案有效
            • @JakubGruber 很高兴有一个解决方案。祝你有美好的一天。
            • 奇怪,但是在 API 27、28 模拟器上,它并没有帮助我避免工具栏周围的阴影。
            猜你喜欢
            • 2016-01-11
            • 2016-04-23
            • 2019-08-04
            • 1970-01-01
            • 2021-02-15
            • 1970-01-01
            • 1970-01-01
            • 2021-03-18
            • 1970-01-01
            相关资源
            最近更新 更多