如何在折叠时更改工具栏图标的颜色 [英] How to change toolbar icons color on collapsing
问题描述
我在项目中使用CoordinatorLayout.它工作正常,但是有一个问题我需要修复.我使用的是白色的操作栏图标,并且当CoordinatorLayout展开并且背景颜色为白色时,我看不到任何这些图标.
I'm using CoordinatorLayout in my project . 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 .
如您所见,展开时,图标变暗,关闭时,图标变白.
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>
我该怎么办?
推荐答案
您可以执行类似于此堆栈溢出帖子中所示的操作.感谢 tachyonflux
You can do this similar to as shown in this Stack Overflow post. Thanks to 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.
这篇关于如何在折叠时更改工具栏图标的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!