【问题标题】:How to make the AppBarLayout have a transparent background like Gmail?如何让 AppBarLayout 像 Gmail 一样有透明背景?
【发布时间】:2021-02-15 17:09:12
【问题描述】:

我正在尝试实现一个类似于Gmail的appbarlayout,但是我有一个小问题,背景不透明。我试图放置透明的颜色背景,但这没有奏效。尝试应用具有透明属性的样式,但它不起作用。所以我用完了替代品,任何帮助都将不胜感激

Gmail 应用栏布局:

我的应用栏布局:

  <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="6dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:fitsSystemWindows="true">

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardElevation="8dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:cardMaxElevation="15dp"
            app:cardCornerRadius="10dp">

       <com.google.android.material.appbar.MaterialToolbar
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

 </com.google.android.material.card.MaterialCardView>

【问题讨论】:

  • 在布局或完整布局中显示recyclerView
  • 是一个片段容器

标签: android android-studio android-layout material-design


【解决方案1】:

尝试在 FrameLayout 中添加这个:

<FrameLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

    <fragment> // here include your fragment container

 <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="6dp"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:fitsSystemWindows="true">

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardElevation="8dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:cardMaxElevation="15dp"
            app:cardCornerRadius="10dp">

       <com.google.android.material.appbar.MaterialToolbar
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

 </com.google.android.material.card.MaterialCardView>

</FrameLayout>

【讨论】:

  • 我已经尝试过了,但不幸的是我会丢失应用程序:layout_scrollFlags = "scroll | enterAlways"。滚动时不隐藏
  • 用 CoordinatorLayout 替换 FrameLayout
【解决方案2】:

使用cardview创建布局而不使用appbar,然后你可以在向下滚动时动画消失并向上滚动以显示卡片

【讨论】:

  • 举个例子比纯文本更有帮助。
【解决方案3】:
 recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
       

        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
     

            if (dy > 0) {
                // Scrolling up

                appBar.setBackgroundResource(R.drawable.bg_white_with_shadow)



            } else if (dx == 0 && layoutManager.findFirstVisibleItemPosition() == 0) {
                // Scrolling down

                appBar.setBackgroundColor(Color.TRANSPARENT)


            }
        }
    })

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多