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

Q

我有一个折叠的工具栏,但是我不能为上帝的爱弄清楚为什么它不改变为我想要的颜色,因为它折叠了...这是我的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>

这是我的android清单。

<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

米格尔·贝尼特斯(Miguel Benitez)

在中添加一个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));
        }
    }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中更改折叠工具栏的文本颜色和折叠工具栏的后退按钮颜色

折叠时淡化工具栏文字颜色

Android Material Design-CollapsingToolbarLayout折叠后如何更改工具栏的背景颜色

更改折叠工具栏的背景

Android:滚动时如何在折叠的工具栏中更改视图

折叠时,在折叠工具栏中设置主题

折叠时折叠工具栏锚定视图被隐藏

折叠工具栏折叠时隐藏浮动按钮

Android折叠工具栏不会折叠

可折叠的工具栏不折叠

Android L联系人应用程序如何折叠其工具栏?

更改可折叠工具栏标题的正确方法

将折叠的工具栏标题更改为微调器

在折叠状态下更改方向时,工具栏标题消失

当RecyclerView适合屏幕时,请不要折叠工具栏

工具栏未在CoordinatorLayout中滚动时折叠

如何在单击按钮时更改工具栏颜色?

在片段更改期间如何存储和恢复折叠工具栏的滚动位置?

如何显示像圆形图像视图一样在折叠时从折叠条移动到工具栏

当AppBar折叠/展开时,如何使工具栏固定在顶部?

滚动时如何在折叠的工具栏中修复视图?

如何在像 WhatsApp 一样展开时模糊折叠工具栏图像?

如何获得折叠的工具栏布局的最大垂直偏移

如何解决折叠的工具栏滚动问题

如何使用 frgagment 实现工具栏折叠/展开?

如何实现Play商店折叠工具栏布局?

如何在Android中删除折叠工具栏

透明的工具栏,用于展开的折叠工具栏

无法从折叠的工具栏中显示工具栏