Android-在运行时以编程方式更改汉堡包/后退图标的颜色 [英] Android - Change Hamburger/Back icons color programmatically during runtime

查看:92
本文介绍了Android-在运行时以编程方式更改汉堡包/后退图标的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以编程方式并在运行时更改应用程序的样式属性"colorControlNormal",但没有任何结果.

I'm trying to change the style attribute "colorControlNormal" of my app programmatically and during runtime, but I didn't have any results.

此属性是将使汉堡包和糖果着色的颜色.新工具栏viewGroup的后退图标.另外,我正在使用v7兼容性库.

This property is the color that will tint the hamburger & back icons of the new Toolbar viewGroup. Beside, I'm using the v7 compatibility library.

我听说我们无法在运行时更改应用程序主题,但是我正在寻找答案,即使方法不是很干净.

I heard that we cannot change app theme during runtime, but I'm looking for an answer, even if it's not so clean way.

我只是想知道gmail正在执行我想要的操作,当您单击搜索图标时,白色汉堡包图标又变成了灰色.

I just figured that gmail is doing what i want, when you click on the search icon, the white hamburger icon turn into grey back.

等待更多.

推荐答案

我花了一天的时间,尝试了不同的实现方式.所以我认为,最好的方法是从AppCompat v7库复制并粘贴DrawerArrowDrawable.

I spent one day, played with different implementation. So my opinion, the best way todo that it copy paste DrawerArrowDrawable from AppCompat v7 library.

https://gist.github.com/IstiN/5d542355935fd7f0f357 -看一下经过优化的代码

https://gist.github.com/IstiN/5d542355935fd7f0f357 - take a look on the code with some optimization

您可以在下面的代码中将其用于主要活动

than you can use it in your main activity with code below

        DrawerArrowDrawable drawable = new DrawerArrowDrawable(this, this);
        ImageView menuButton = (ImageView) findViewById(R.id.arrow);
        menuButton.setImageDrawable(drawable);
        menuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((DrawerLayout)findViewById(R.id.drawer)).openDrawer(Gravity.START);
            }
        });

开始新片段时,您需要在同一位置创建一个视图,并将第二个代码添加到片段中

when you start new fragment, you need to create one more view on the same place and add second code to your fragment

    private DrawerArrowDrawable mArrowDrawable;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mArrowDrawable = new DrawerArrowDrawable(getActivity(), getActivity());
        ImageView topButton = (ImageView) view.findViewById(R.id.arrow);
        topButton.setImageDrawable(mArrowDrawable);
        topButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeSearch();
            }
        });

        //run animation from hamburger to arrow
        animate(0, 1, null);
        ....

    private void animate(int startValue, int endvalue, Animator.AnimatorListener listener) {
        ValueAnimator anim = ValueAnimator.ofFloat(startValue, endvalue);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float slideOffset = (Float) valueAnimator.getAnimatedValue();
                mArrowDrawable.setProgress(slideOffset);
            }
        });
        anim.setInterpolator(new DecelerateInterpolator());
        anim.setDuration(300);
        if (listener != null) {
            anim.addListener(listener);
        }
        anim.start();
    }

制作从箭头到汉堡的动画手柄后退按钮并执行代码

to make animation from arrow to hamburger handle back button and execute code

animate(1, 0, null);

您还需要在片段中等待动画不会结束,但这是另一个问题.

you also need to wait in your fragment while animation will not finish, but it another questions.

如果您有任何疑问,请在评论中提问.

If you have any questions ask in comments.

这篇关于Android-在运行时以编程方式更改汉堡包/后退图标的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆