简体   繁体   English

如何在折叠时更改工具栏图标颜色

[英]How to change toolbar icons color on collapsing

I'm using CoordinatorLayout in my project . 我在我的项目中使用CoordinatorLayout。 It works fine but there is a problem I need to fix .I'm using white actionbar icons and When the CoordinatorLayout is expanded and the background color is white, I can't see any of those icons . 它工作正常,但我需要修复一个问题。我正在使用白色操作栏图标,当CoordinatorLayout展开并且背景颜色为白色时,我看不到任何这些图标。

I saw this in an application . 我在一个应用程序中看到了这个 when the CoordinatorLayout is expanded , the icons are dark and when scrolling down, they begin to change into white . 当CoordinatorLayout展开时,图标会变暗,当向下滚动时,它们会变为白色。

http://i.stack.imgur.com/rCCQb.png

as you can see, when it's expanded the icons are dark and when it closes , the icons becomes white . 正如您所看到的,当它被展开时,图标是黑暗的,当它关闭时,图标变为白色。

it begins to change when scroll down until it completely convert from dark icons to white icons 向下滚动直到它完全从黑暗图标转换为白色图标开始变化

sorry for not attaching the code , here is my codes : 抱歉没有附上代码,这是我的代码:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="240dp"
    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:fitsSystemWindows="true"
        app:contentScrim="#FF4548"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <RelativeLayout
            android:id="@+id/slidelayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="?attr/actionBarSize"
            android:background="#fff">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/indicator"
                android:layout_width="fill_parent"
                android:layout_height="10dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="7dp" />
        </RelativeLayout>

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:paddingLeft="3dp"
            android:layout_gravity="top"
            android:paddingRight="3dp"
            app:layout_collapseMode="pin">

            <ImageView
                android:id="@+id/imgsearch"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="2dp"
                android:paddingBottom="5dp"
                android:src="@drawable/ic_action_search"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/imgshare"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/imgsearch"
                android:paddingBottom="5dp"
                android:src="@drawable/ic_action_share"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/imglike"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="5dp"
                android:layout_toRightOf="@+id/imgshare"
                android:paddingBottom="5dp"
                android:src="@drawable/ic_action_favorite"
                android:visibility="visible" />

            <RelativeLayout
                android:id="@+id/sabadkharidslayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/imglike"
                android:visibility="gone">

                <ImageView
                    android:id="@+id/img_sabad"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/sabad" />

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_marginBottom="15dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/circle">

                    <TextView
                        android:id="@+id/text_numkharid"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerHorizontal="true"
                        android:gravity="right"
                        android:includeFontPadding="false"
                        android:singleLine="true"
                        android:textColor="#fff"
                        android:textSize="12sp" />
                </RelativeLayout>
            </RelativeLayout>

            <ImageView
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="5dp"
                android:paddingBottom="5dp"
                android:rotationY="180"
                android:src="@drawable/ic_action_back"
                android:visibility="visible" />

            <TextView
                android:id="@+id/textVieww1"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_marginRight="5dp"
                android:layout_toLeftOf="@+id/back"
                android:gravity="right|center_vertical"
                android:shadowColor="#eee"
                android:shadowDx="2"
                android:shadowDy="2"
                android:shadowRadius="4"
                android:singleLine="true"
                android:textColor="#fff"
                android:textSize="17sp" />

            <AutoCompleteTextView
                android:id="@+id/editText1"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/textVieww1"
                android:layout_toRightOf="@+id/sabadkharidslayout"
                android:background="#fff"
                android:completionThreshold="1"
                android:dropDownWidth="match_parent"
                android:ems="10"
                android:singleLine="true"
                android:gravity="right"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:textSize="12sp"
                android:visibility="gone">

                <requestFocus />
            </AutoCompleteTextView>
        </RelativeLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

How can I do so ? 我怎么能这样做?

You can do this similar to as shown in this Stack Overflow post . 您可以执行此操作,类似于此Stack Overflow帖子中所示 Thanks to tachyonflux 感谢tachyonflux

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
AppBarLayout appBar = (AppBarLayout)  findViewById(R.id.appbar);

appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if ((collapsingToolbar.getHeight() + verticalOffset) < (2 * ViewCompat.getMinimumHeight(collapsingToolbar))) {
                toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
            } else {
                toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP);
            }
        }
    });

Similarly you can apply it for any drawable or images within toolbar or collapsing toolbar layout. 同样,您可以将其应用于工具栏或折叠工具栏布局中的任何可绘制图像或图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM