【发布时间】:2017-12-12 11:43:29
【问题描述】:
我在我的项目中使用 CollapsingToolbarLayout。
CollapsingToolbarLayout展开时,默认为白色(即在style.xml中定义为android:textColorSecondary)。
我的问题:
我想更改我的菜单项图标颜色。
xml 文件:
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
MainActivity 代码:
AppBarLayout appBarLayout = findViewById(R.id.appbar_layout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
//collapse map
//TODO: change share icon color - set white share icon
isShow = true;
} else if (isShow) {
//expanded map
//TODO: change share icon color - set dark share icon
isShow = false;
}
}
});
到目前为止我所尝试的:
设置toolbar.getNavigationIcon().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); 但它会更改返回按钮颜色,而我想更改共享按钮图标颜色。
DrawableCompat.setTint 在我的情况下不起作用。
谢谢。
【问题讨论】:
标签: android android-layout android-collapsingtoolbarlayout