代码之家  ›  专栏  ›  技术社区  ›  Neon Warge user547852

后退按钮未显示在折叠工具栏布局中

  •  7
  • Neon Warge user547852  · 技术社区  · 9 年前

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

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

    下面是它当前的样子:

    enter image description here

    现在崩溃了,标题明显偏右了。按钮在那里,我可以点击它,它会返回到我的应用程序的主屏幕,但背面图标不在那里。

    enter image description here

    以下是活动的布局文件:

    <?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版本23.0.2,我的所有支持库都是23.1.0

    3 回复  |  直到 9 年前
        1
  •  8
  •   Neon Warge user547852    9 年前

    我找到了答案,看来教程不适合我的情况,因为他们可能在如何加载图像背景上使用了不同的方法。我只需将我的文件放入可绘制的xxxhdpi文件夹中,然后通过 android:src 我不太确定这是否有区别。我偷看了一下样品 project 并且它们在运行时延迟图像的加载/缓存 onCreateView() 使用 Glide 。我没有这么做。我想这是关于素材背景的巨大文件大小的惯例?

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

    所以我把ImageView放在工具栏下面。这一次,工具栏首先显示,然后显示图像。它奏效了!

    这是我的新布局:

    <?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>
    

    我在想,如果我可以延迟一点图像的加载(比如延迟 打开创建视图() )让工具栏先出现,也许我如何在CollapsingToolbarLayout中定位元素并不重要。但这只是我粗略的猜测。我可能总是错的。

        2
  •  6
  •   Madi    8 年前

    只需使用 app:layout_collapseMode="pin" 内部工具栏!很好)谢谢

        3
  •  2
  •   Petr Tykal    8 年前

    我已更改了内部版本。渐变自

    编译'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);
    

    现在后退按钮工作了!