使用appcompat-v7更改Actionbar中的返回箭头图像

3

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

我在某个地方读到要使用setHomeAsUpIndicator()方法更改它,但它会更改汉堡按钮,而且它没有动画到返回箭头。


1
图像可视化将会很有帮助。 - Aminah Nuraini
3个回答

2

至少有六种方法可以实现这一点,但可能最简单和最短的方法是使用反射来抓取 ActionBarDrawerToggleDrawable 并翻转其方向。

该示例将该功能封装在子类中,并应适用于您的任何 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翻转,您应该相应地更改布局的方向。


没问题。如果你没有注意到,我在最初发布代码后进行了一些更改。我忘记了有一个接受Toolbar的构造函数,并且我使用了一个只在19+可用的方向常量。现在应该可以了。干杯! - Mike M.
另一个问题:您知道如何在 API 级别低于 17 的情况下将 layoutdirection 设置为 RTL 吗?因为现在汉堡包图像位于操作栏的左侧。 - Maryam
很遗憾,在API 17之前,你必须自己处理那些东西。如果你使用Toolbar而不是提供装饰的ActionBar,那么会更容易一些,因为Toolbar只是一个常规的ViewGroup,可以在XML中设置。在这种情况下,ActionBarDrawerToggle并不是你想要的,因为它会固定在左侧。然而,使用ImageButtonDrawerArrowDrawable,你可以创建一个外观和行为相同的自定义切换,并将其放置在Toolbar的右侧。在DrawerLayout中,只需将抽屉的layout_gravity设置为right即可。 - Mike M.
在这种情况下,你的意思是我别无选择了吗? - Maryam
我不确定你的意思,但据我所知,在API 17之前,没有办法仅通过一个简单的属性(如layoutDirection)来镜像RTL布局。您需要使用自定义布局和切换,但如果您使用Toolbar,这并不太困难。 - Mike M.

0

试试这个:

//Find the action bar for customizations
    final ActionBar ab = getSupportActionBar();
    assert ab != null;

    //Make the action bar display an up arrow and set its drawable and color
    ab.setDisplayHomeAsUpEnabled(true);
    final Drawable upArrow = ResourcesCompat.getDrawable(
            getResources(),
            R.drawable.abc_ic_ab_back_mtrl_am_alpha, //this is the <- arrow from android resources. Change this to the thing you want.
            null);
    assert upArrow != null;
    upArrow.setColorFilter(
            ContextCompat.getColor(
                    getApplicationContext(),
                    R.color.white // change this to the color you want (or remove the setColorFilter call)
            ),
            PorterDuff.Mode.SRC_ATOP);
    ab.setHomeAsUpIndicator(upArrow);

我在我的问题中说过,我测试了 setHomeAsUpIndicator。但它会移除动画,只是改变汉堡图标。 - Maryam

0
mDrawerToggle = new ActionBarDrawerToggle...mDrawerToggle.syncState(); 之间添加以下条件,如下所示:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL)
{
      toolbar.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}


mDrawerToggle.syncState();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接