【问题标题】:back button not showing up in CollapsingToolbarLayoutCollapsingToolbarLayout 中未显示后退按钮
【发布时间】:2016-06-19 07:17:12
【问题描述】:

您好,我正在做一个简单的应用程序来尝试材料设计。不幸的是,按照令人难以置信的教程here 并尝试了示例here(这些是材料设计中仅供参考的令人难以置信的资源),我正在努力使我的应用程序工具栏能够正常工作。例如,折叠时标题旁边的后退按钮不会显示。但是我可以简单地单击它应该在的区域并做出响应。

因此,按钮在那里,但没有出现,我不知道为什么。此外,当工具栏折叠时,工具栏不会将其颜色更改为我设置的原色。 contentScrim 似乎对折叠模式没有反应,但我会为此发布另一个问题。

这是它目前的样子:

现在折叠时,标题明显偏右。按钮在那里,我可以点击它,它返回到我的应用程序的主屏幕,但没有返回图标。

这是活动的布局文件:

<?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:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".HomeScreenActivity"
    android:weightSum="1">

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

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

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

            <ImageView
                android:src="@drawable/backgroud_note"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                app:layout_collapseMode="parallax"/>

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        ...    
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_note"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/appbar_note"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_send_white_24dp" />

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

我正在使用 API 22 build 23.0.2 我所有的支持库都在 23.1.0

【问题讨论】:

    标签: android android-layout material-design android-coordinatorlayout android-collapsingtoolbarlayout


    【解决方案1】:

    我找到了答案,看来本教程不适用于我的情况,因为他们可能使用不同的方法来加载图像背景。我只是将我的放入 drawable-xxxhdpi 文件夹并通过android:src 属性加载它我不太确定这是否会有所不同。我查看了示例project,他们使用GlideonCreateView() 中延迟了运行时图像的加载/缓存。我不这样做。我猜这是关于材质背景的大文件大小的约定?

    在我看来,这涉及到实际布局的定位方式。我将 ImageView 放置在工具栏下方。所以我相信首先渲染图像视图。即使输入了工具栏,ImageView 也始终存在。这一定是 contentScrim 看起来无法识别它已经处于工具栏模式的原因。 ImageView 阻塞了整个 Toolbar!

    所以我将 ImageView 放在了 Toolbar 的下方。这一次,工具栏首先出现,然后是图像。成功了!

    这是我的新布局:

    <?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:fitsSystemWindows="true"
        android:orientation="vertical"
        tools:context=".HomeScreenActivity"
        android:weightSum="1">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar_note"
            android:layout_width="match_parent"
            android:layout_height="168dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:fitsSystemWindows="true">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingtoolbarlayout_note"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <ImageView
                    android:src="@drawable/backgroud_note"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:fitsSystemWindows="true"
                    app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
                    app:layout_collapseMode="parallax"/>
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar_note"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_scrollFlags="scroll|enterAlways"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="pin" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            ...    
        </android.support.v4.widget.NestedScrollView>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_anchor="@id/appbar_note"
            app:layout_anchorGravity="bottom|right|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_send_white_24dp" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    我在想,如果我可以稍微延迟一下图像的加载(比如推迟onCreateView() 的图像加载),让工具栏先出现,也许我如何在里面放置我的元素并不重要折叠工具栏布局。但这只是我的粗略猜测。我总是错的。

    【讨论】:

    • 谢谢,我的 layout_collapseMode 反之亦然,这就是造成问题的原因
    【解决方案2】:

    只需在工具栏内使用app:layout_collapseMode="pin"!它工作正常)谢谢

    【讨论】:

      【解决方案3】:

      我已经从

      更改了 build.gradle 中的一个版本

      编译'com.android.support:design:22.2.0'

      编译'com.android.support:design:24.2.1'

      并将其添加到类中:

          Toolbar toolbar = (Toolbar) findViewById(R.id.mainToolbar);
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayShowTitleEnabled(true);
      

      现在后退按钮可以使用了!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多