【问题标题】:Toolbar showing even with background transparent in CollapsingToolbarLayout [duplicate]在 CollapsingToolbarLayout 中,即使背景透明,工具栏也会显示 [重复]
【发布时间】:2016-03-18 12:31:28
【问题描述】:

我有许多在详细信息片段上使用 CollapsingToolbarLayout 的应用程序,但我试图将其放在 List (RecyclerView) 片段上。在 RecyclerView 片段上,工具栏在 CTL 上方是不透明的,而不是透明并允许图像显示在下方。

Fragment 放置在 FrameLayout 中:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/application_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark">

    <FrameLayout
        android:id="@id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"/>

</RelativeLayout>

片段布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <android.support.design.widget.AppBarLayout
        android:id="@id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleMarginEnd="64dp"
            android:fitsSystemWindows="true">

            <ImageView
                android:id="@id/toolbar_image"
                android:src="@drawable/header_image"
                android:contentDescription="@string/header_image"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimaryTransparent"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:clickable="true" />

</android.support.design.widget.CoordinatorLayout>

在 Android Studio 中,设计器看起来不错,而且应该如此(图 1);但在我的手机和模拟器上,它看起来像(图 2)。

图片 1 - AS 设计布局

图片 2 - 手机 SS

【问题讨论】:

  • @color/colorPrimaryTransparent的值是多少?
  • 它的 #00brown,我现在忘记了确切的数字。我不得不走开,这让我发疯了。
  • 我认为这可以满足您的需求。作为旁注,您应该将 RecyclerView 放入 NestedScrollView
  • 不确定 NSV 能否正常工作。 [我想我提到过,不记得]我有两个片段,一个带有 RecyclerView(列表),一个带有 NestedScrollView(详细信息),工具栏显示在它们上方。我可以试一试,但这并不能解释为什么我的细节片段显示相同。我非常感谢您的帮助,这让我今晚发疯了,但我不认为 Actionbar 链接是相同的,因为我正在尝试使用应该可以正常工作的支持/设计视图。谢谢。
  • 好的,但是您正在尝试使用淡入纯色的图像来实现折叠工具栏,对吧?

标签: android toolbar android-collapsingtoolbarlayout


【解决方案1】:

如果您希望工具栏透明,请使用@android:color/transparent 代替@color/colorPrimaryTransparent。

更新答案:

我已经尝试过您的代码。尝试为您的 AppBarLayout 添加一些高度,例如 200p,并使 ImageView 的高度与父级匹配。我已经在运行 API 23 (Marshmallow) 的模拟器上对其进行了测试,它隐藏了工具栏并且运行良好。

这是我尝试过的代码:

请注意:因为我没有您的可绘制文件,所以我使用了 ic_launcher 并将其从 src 更改为背景,只是为了在我的模拟器上获得更好的用户界面。

<android.support.design.widget.CoordinatorLayout           xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/toolbar_image"
            android:background="@mipmap/ic_launcher"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:clickable="true" />

</android.support.design.widget.CoordinatorLayout>

【讨论】:

  • 谢谢伙计,但一直在那里尝试过。没有骰子。我的列表碎片是我的自定义颜色#00brown,我的详细信息碎片是 android:color/trans 并且工具栏都显示在它们上面。
  • 我尝试了一些方法并更新了我的答案。请看一看。如有任何疑问或疑问,请随时提问。
  • 希望这能解决您的问题。
  • 试一试.. AppBar 200dp,CTL 匹配,IV 匹配。工具栏背景颜色有问题。如果我把它染成绿色,它就会变成绿色,但如果我把背景改成绿色,它就不会改变。仍然是图像视图上方的原色工具栏。
  • 当我们向项目添加支持库时,android now-a-days 默认提供支持库的 alpha 版本(24.0.0-alpha)。确保您没有运行支持库的 alpha 版本。他们挤满了虫子。如果您使用的是 alpha 版本,只需在您的 gradle 文件中将它们更改为 23.2.1。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
相关资源
最近更新 更多