使用 appcompat-v7 更改操作栏中的后退箭头图像

2023-12-02

我有一个Actionbar from android.support.v7.widget.Toolbar。它有带有向后箭头动画的汉堡包图像,我想将向后箭头从 。我怎样才能在 Android Studio 中做到这一点?

我在某处读到要更改它setHomeAsUpIndicator()方法,但它改变了汉堡按钮,并且没有向后箭头的动画。


至少有六种方法可以做到这一点,但最简单和最短的可能是使用反射来获取ActionBarDrawerToggle's Drawable,并翻转其方向。

此示例将该功能包装在子类中,并且无论您的情况如何,都应该可以工作ActionBar/Activity设置(前提是原来的班级首先在那里工作)。

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要翻转,您应该相应地更改布局的方向。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 appcompat-v7 更改操作栏中的后退箭头图像 的相关文章

随机推荐