android - CollapsingToolBarLayout - 状态栏稀松布颜色不会改变

标签 android android-toolbar android-6.0-marshmallow android-collapsingtoolbarlayout

几天前我更新了我的 android 工作室并开始使用 CoordinatorLayout 和 CollapsingToolbarLayout 只是尝试一些东西。

似乎工具栏稀松布颜色覆盖了状态栏初始颜色和状态栏稀松布颜色(从 xml 和代码都尝试过)

初始状态:

initial state:


开始滚动:

started scrolling


滚动到折叠:

scrolled until collapsing


所以问题是:

  1. 如何防止工具栏在折叠时覆盖状态栏(甚至不让我正在折叠的图像超出它)。

  2. 折叠后如何更改状态栏颜色

我遇到的另一个问题是我给工具栏提供了初始颜色,而不仅仅是稀松布颜色,因为我希望工具栏位于图片上方,但它会出现在图片的顶部,并会覆盖其中的一部分以及任何内容那会在它后面崩溃


在 xml 中为工具栏添加颜色/样式:

added colour/style to the toolbar in the xml


  1. 有没有什么办法可以让工具栏从一开始就放在图片上方,然后只收起图片? (考虑过可能保持工具栏固定并在图像之前进行框架布局,但它仍然会在状态栏区域崩溃,这是主要问题。)

主要 Activity xml:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim = "?attr/colorPrimary"
        app:statusBarScrim="?attr/colorAccent" --------> not changing
        android:id="@+id/my_ctl">


        <ImageView
            android:id="@+id/image"
            android:src="@drawable/flights"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            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"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            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="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <include layout="@layout/content_main" />

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

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

MainActivity.java:

  CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.my_ctl);
    //ctl.setContentScrimColor(Color.RED);
    ctl.setStatusBarScrimColor(Color.BLUE);  --------> not working
    ctl.setTitle("blabla");

最佳答案

更改app:statusBarScrim="?attr/colorAccent" CollapsingToolbarapp:statusBarScrim="@android:color/transparent"

然后你可以设置CollapsingToolbar的颜色为ctl.setStatusBarScrimColor(Color.BLUE);在你的代码中

还要确保添加 android:fitsSystemWindows="true"AppBarLayout

关于android - CollapsingToolBarLayout - 状态栏稀松布颜色不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354261/

相关文章:

Java解析Json字节数组

android - 在 fragment 中创建工具栏

android - 如何避免在 Android M 上处于电池/省电模式时停止我的前台服务

android - Gradle 同步在全新安装的 Android Studio、Ubuntu 16.04 上失败

java - Jsoup 在第一个元素之后选择元素

android - 如何在工具栏的垂直中心实现后退箭头(向上按钮)

android-5.0-lollipop - android Lollipop 工具栏: how to hide/show the toolbar while scrolling?

android - 读取外部存储时找不到文件异常

java.lang.IllegalStateException : HTC Android 6. 0 棉花糖崩溃 - Google Play 快照

java - Android 在 X 次尝试失败后阻止登录