使用appcompat-v7在操作栏中更改后退箭头图像 [英] change back arrow image in Actionbar with appcompat-v7

查看:88
本文介绍了使用appcompat-v7在操作栏中更改后退箭头图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自android.support.v7.widget.ToolbarActionbar.它具有带有向后箭头动画的汉堡包图像,我想将向后箭头从<-更改为->.如何在Android Studio中做到这一点?

I have an Actionbar from android.support.v7.widget.Toolbar. It has hamburger image with animation to back arrow, I want to change back arrow from <- to ->. how can I do this in Android Studio?

我在某处阅读过要使用setHomeAsUpIndicator()方法对其进行更改,但是它更改了汉堡包按钮,并且没有向后箭头的动画.

I read somewhere to change it with setHomeAsUpIndicator() method, but it change hamburger button and it has no animation to back arrow.

推荐答案

至少有六种方法可以做到这一点,但可能最简单,最短的方法是使用反射来抓取ActionBarDrawerToggleDrawable ,然后反转方向.

There are at least half a dozen ways to do this, but probably the simplest and shortest is to use reflection to grab the ActionBarDrawerToggle's Drawable, and flip its direction.

此示例将功能包装在子类中,无论您使用ActionBar/Activity设置如何,它都应该起作用(前提是原始类首先在那里工作).

This example wraps that functionality in a subclass, and should work no matter your ActionBar/Activity setup (provided the original class worked there in the first place).

public class FlippedDrawerToggle extends ActionBarDrawerToggle {
    public FlippedDrawerToggle(Activity activity, DrawerLayout drawerLayout,
        int openDrawerContentDescRes, int closeDrawerContentDescRes) {

        this(activity, drawerLayout, null,
            openDrawerContentDescRes, closeDrawerContentDescRes);
    }

    public FlippedDrawerToggle(Activity activity, DrawerLayout drawerLayout,
        Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {

        super(activity, drawerLayout, toolbar,
             openDrawerContentDescRes, closeDrawerContentDescRes);

        try {
            Field sliderField = ActionBarDrawerToggle.class.getDeclaredField("mSlider");
            sliderField.setAccessible(true);
            DrawerArrowDrawable arrow = (DrawerArrowDrawable) sliderField.get(this);
            arrow.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_RIGHT);
        }
        catch (NoSuchFieldException | IllegalAccessException e) {
            // Fail silently
        }
    }
}

这只会更改切换图像的方向.如果您实际上希望翻转整个ActionBar/Toolbar,则应相应地更改布局的方向.

This will only change the direction of the toggle's image. If you actually want the whole ActionBar/Toolbar to be flipped, you should instead change the layout's direction accordingly.

这篇关于使用appcompat-v7在操作栏中更改后退箭头图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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