android - CollapsingToolbarLayout : Change home button color when expanded

标签 android android-collapsingtoolbarlayout

我已经从 Chris Banes 示例代码中实现了新的 CollapsingToolbarLayout

但是,背景 ImageView 的图像都是白色背景。工具栏主题是 ThemeOverlay.AppCompat.Dark.ActionBar 所以图标也是白色的,所以当 CollapsingToolbarLayout 完全展开时我看不到主页按钮。

使用 app:expandedTitleTextAppearance 我可以设置标题字段的颜色。是否也可以设置主页按钮和菜单图标的颜色?

最佳答案

这是我如何在布局展开和折叠时更改抽屉和选项图标颜色的示例:

protected void onCreate(Bundle savedInstanceState) {
            AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
            appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int offset)
                {
                    Drawable upArrow = ResourcesCompat.getDrawable(getResources(), R.drawable.drawer_icon, null);
                    if (offset < -200)
                    {
                        upArrow.setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
                        getSupportActionBar().setHomeAsUpIndicator(upArrow);

                        Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.option_menu_icon);
                        drawable.setColorFilter(Color.parseColor("#000000"), PorterDuff.Mode.SRC_ATOP);
                        toolbar.setOverflowIcon(drawable);
                    }
                    else
                    {

                        upArrow.setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
                        getSupportActionBar().setHomeAsUpIndicator(upArrow);
                        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

                        Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.option_menu_icon);
                        drawable.setColorFilter(Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
                        toolbar.setOverflowIcon(drawable);
                    }
        }
});

关于android - CollapsingToolbarLayout : Change home button color when expanded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30936373/

相关文章:

c# - Unity无法使用Admob解决

android - Textinput (React Native - Android) - 支持 onKeyPreIme

java - 坐标布局中 enterAlwaysCollapsed 和 exitUntilCollapsed 滚动标志的区别

android - 为什么折叠工具栏标题在工具栏下面?

android - 使用CoordinatorLayout时如何隐藏工具栏的部分内容

android - 如何将顶 View 折叠成较小尺寸的 View ?

java - 基本的第二个定时器实现

java - 如何为后续与 HttpClient 的连接存储 cookie

java - Android 中如何刷新 ListView ?

Android 折叠工具栏 : How to resize the text, 使其显示全文而非部分