【问题标题】:Change the color of menu items as appBarLayout is being collapsed折叠 appBarLayout 时更改菜单项的颜色
【发布时间】:2018-02-18 17:33:07
【问题描述】:

在 appBarLayout 折叠时更改工具栏背景颜色 我用过下面的代码

AppBarLayout appBarLayout = findViewById(R.id.app_bar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        float offset = (float) Math.abs(verticalOffset) / appBarLayout.getTotalScrollRange();
        toolbar.getBackground().setAlpha((int) (offset * 255));
    }
});

现在我想更改 appBarLayout 折叠时工具栏上的菜单项的颜色,如下例所示

Expected Result

【问题讨论】:

    标签: android toolbar menuitem android-appbarlayout


    【解决方案1】:
    Menu mItem;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mItem = menu;
    
        getMenuInflater().inflate(R.menu.bookmark_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    

    获取menu的引用。在你的offsetchangelistener中

    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener()
            {
                @Override
                public void onOffsetChanged (AppBarLayout appBarLayout,int verticalOffset){
                float offset = (float) Math.abs(verticalOffset) / appBarLayout.getTotalScrollRange();
                toolbar.getBackground().setAlpha((int) (offset * 255));
                mItem.findViewById(R.id.example).getIcon().setColorFilter(ContextCompat.getColor(ActivityHome.this, R.color.yourcolor), PorterDuff.Mode.SRC_IN);
            }
            });
    

    【讨论】:

    • 这将如何运作?您没有使用偏移值。 MenuItem 对象也没有 setColorFilter 函数。
    • mItem.findViewById(R.id.example).getIcon.setColorFilter 是正确的。
    • 它没有帮助,甚至没有关闭。实际上只是浪费了我的时间。无论如何感谢您的回答。
    猜你喜欢
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多