簡體   English   中英

如何在折疊時更改工具欄圖標顏色

[英]How to change toolbar icons color on collapsing

我在我的項目中使用CoordinatorLayout。 它工作正常,但我需要修復一個問題。我正在使用白色操作欄圖標,當CoordinatorLayout展開並且背景顏色為白色時,我看不到任何這些圖標。

我在一個應用程序中看到了這個 當CoordinatorLayout展開時,圖標會變暗,當向下滾動時,它們會變為白色。

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

正如您所看到的,當它被展開時,圖標是黑暗的,當它關閉時,圖標變為白色。

向下滾動直到它完全從黑暗圖標轉換為白色圖標開始變化

抱歉沒有附上代碼,這是我的代碼:

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

我怎么能這樣做?

您可以執行此操作,類似於此Stack Overflow帖子中所示 感謝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);
            }
        }
    });

同樣,您可以將其應用於工具欄或折疊工具欄布局中的任何可繪制圖像或圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM