当折叠的工具栏折叠时,如何更改其颜色? [英] How do you change the color of collapsing toolbar when it's collapsed?

查看:63
本文介绍了当折叠的工具栏折叠时,如何更改其颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个折叠的工具栏,但是我不能为上帝的爱弄清楚为什么它不改变为我想要的颜色,因为它折叠了...这是我的xml代码折叠的工具栏.

I have a collapsing toolbar, but I cannot for the love of god figure out why its not changing to the color i want it to as it collapses...Here is my xml code the collapsing 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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/FrontierTheme">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:src="@drawable/background"
                android:id="@+id/profileView_imageView"/>
            <android.support.v7.widget.Toolbar
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent" android:layout_height="wrap_content"
                android:id="@+id/toolbarProfileViewID"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/FrontierTheme"
                app:layout_collapseMode="pin">
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/mainColor"
            app:tabMode="scrollable"
            app:tabTextColor="@color/white"/>
    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/tab_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

这是我的styles.v21(常规的styles.xml文件中没有任何内容)

Here is my styles.v21 (i dont have have anything in the regular styles.xml file)

<style name="FrontierTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorPrimary">#5F021F</item>
        <item name="android:colorPrimaryDark">#5E152C</item>
        <item name="android:colorAccent">#BCBCBC</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

这是我的android清单.

And here is my android manifest.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.daprlabs.swipedeck" >
 <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="23" />
  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/FrontierTheme"
        android:name=".ApplicationEnvironment">
        <activity
            android:name=".ActivityCenter"> <!--android:theme="@style/TestTheme" -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 <activity android:name=".ProfileView.ProfileView"
            android:label="Frontier" />
   </application>

</manifest>

当前,当它折叠时,颜色是白色...但是我希望它是十六进制的:#5F021F

Currently, when it collapses, the color is white...But i want it to be this hexidecimal: #5F021F

推荐答案

AppBarLayout中添加一个addOnOffsetChangedListener侦听器,以了解其是否折叠,并使用setBackgroundColor更改颜色.像这样:

add a addOnOffsetChangedListener listener in the AppBarLayout to know if it's collapse or not, and change the color using setBackgroundColor. Like this:

//Set a listener to know the current visible state of CollapseLayout
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    int scrollRange = -1;

    @Override
    public void onOffsetChanged(final AppBarLayout appBarLayout, int verticalOffset) {
        //Initialize the size of the scroll
        if (scrollRange == -1) {
            scrollRange = appBarLayout.getTotalScrollRange();
        }
        //Check if the view is collapsed
        if (scrollRange + verticalOffset == 0) {
            toolbar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.YOUR_COLLAPSED_COLOR));
        }else{
            toolbar.setBackgroundColor(ContextCompat.getColor(mContext, R.color.OTHER_COLOR));
        }
    }
});

这篇关于当折叠的工具栏折叠时,如何更改其颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆