折叠工具栏折叠时如何更改折叠工具栏的颜色?

时间:2016-08-01 13:55:36

标签: android android-xml android-toolbar android-collapsingtoolbarlayout

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

<?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文件中没有任何内容)

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

这是我的机器人清单。

<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

2 个答案:

答案 0 :(得分:14)

addOnOffsetChangedListener中添加AppBarLayout侦听器以了解其是否崩溃,并使用setBackgroundColor更改颜色。像这样:

//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));
        }
    }
});

答案 1 :(得分:1)

选择的答案对我不起作用,在我的情况下看起来很糟糕,当verticalOffset == 0时颜色发生变化,所以我尝试了这个:

collapsingToolbarLayout.setContentScrimColor(Color.parseColor("#000000")); // #000000 is black color hex (for example)

现在在折叠时改变颜色看起来光滑干净,

希望它有所帮助,

相关问题