【问题标题】:Coordinator Layout content is showing behind the status bar协调器布局内容显示在状态栏后面
【发布时间】:2015-12-29 16:15:35
【问题描述】:

我正在构建一个包含地图片段的工具栏。工具栏位置固定且不透明。

每当视图的内容向上滚动时,地图片段都会随之移动,但它在工具栏上方可见(因此在状态栏本身和状态栏的橙色背景之间)。

这是由此产生的问题:

如果我再滚动一点,稀松布会正常淡入,但为时已晚。

有没有办法在工具栏上方不绘制内容?还是一种让状态栏背景绘制在 AppBarLayout 内容前面的方法?

我尝试在 AppBarLayout 前面(状态栏正下方)绘制一个视图,但是一旦 MapFragment 一直向上滚动,它就会被带到该视图的前面,使 MapFragment 在一小段时间内可见瞬间。

<?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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/promotion_details_image_height"
        android:clickable="true"
        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"
        android:background="@color/orange"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?attr/actionBarSize">

            <fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="none"
                tools:context=".restaurants.stores.StoresFragment" />

            <ImageView
                android:id="@+id/minimizeBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/stores_details_minimize_margin"
                android:clickable="true"
                android:contentDescription="@null"
                android:paddingTop="@dimen/status_bar_height"
                android:src="@drawable/selector_map_minimize_btn"
                android:visibility="gone" />

            <include
                android:id="@+id/cardDetailsLayout"
                layout="@layout/store_card_small_detail"
                android:layout_width="match_parent"
                android:layout_height="@dimen/stores_details_card_height"
                android:layout_alignParentBottom="true"
                android:layout_margin="10dp"
                android:visibility="gone" />
        </RelativeLayout>

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

            <com.app.views.FontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="@string/store_toolbar_text"
                android:textColor="@color/white"
                android:textSize="@dimen/toolbar_text_size"
                app:font="@string/fontFlamaMedium" />
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

编辑: 在布局 xml 中与我的 RecyclerView 关联的 android:layout_behavior 属性,但工具栏的内容具有正确的行为,问题是它在状态栏下可见,我不希望它。

【问题讨论】:

  • 你的活动主题设置android:statusBarColor是否透明?
  • @cziemba:是的,透明和不透明我都试过了,结果还是一样
  • 您是否尝试过在 xml 中的地图片段中添加android:fitsSystemWindows="true"
  • @MidhunMP 在我分享的 xml 代码中,我确实有与我的 RecyclerView 关联的 android:layout_behavior 属性,就像它被提议作为该问题的解决方案一样,但问题有所不同。工具栏的内容具有正确的行为,它只是在状态栏下方可见,当我不想要它时。

标签: android toolbar android-collapsingtoolbarlayout


【解决方案1】:

你应该删除你的论点:android:fitsSystemWindows="true"

更多关于这个的博客帖子来自谷歌开发者: https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec

【讨论】:

    【解决方案2】:

    从工具栏中删除此属性

    android:background="?attr/colorPrimary"
    

    并将此属性添加到 CollapsingToolbar

     app:contentScrim="?attr/colorPrimary"
    

    contentScrim 将控制工具栏的颜色,并确保颜色仅在背景图像淡出时出现

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-13
      • 2015-12-02
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多