2016-06-12 9 views
3

Actionbarandroid.support.v7.widget.Toolbarです。それはバック矢印にアニメーションのハンバーガーイメージを持っています、私は< - >から戻る矢印を変更したいです。 Android Studioでどうすればいいですか?アクションバーのappcompat-v7で戻る矢印の画像

setHomeAsUpIndicator()メソッドで変更するのはどこか読んでいますが、ハンバーガーボタンが変わり、戻る矢印のアニメーションがありません。これを行うには

あり
//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); 
+1

を画像の可視化は、 –

答えて

2

少なくとも半ダースの方法を、おそらく最も簡単かつ最短はActionBarDrawerToggleDrawableをつかむためにリフレクションを使用し、その方向を反転することです:

+0

ありがとうございます:) – Maryam

+0

問題ありません。あなたが気付かなかった場合は、もともと私が投稿して以来、私はコードのいくつかを変更しました。私は 'Toolbar'を取るコンストラクタを忘れていました。私は19+でしか利用できない方向定数を使っていました。今は行くのがいいと思う。乾杯! –

+0

別の質問:17より下のAPIレベルで 'layoutdirection'を' RTL'に設定する方法を知っていますか?今すぐハンバーガーのイメージがアクションバーの左側にあるので – Maryam

0

これを試してみてください。

この例では、その機能をサブクラスにラップしています。最初のクラスでは、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全体を反転させたい場合は、代わりにレイアウトの方向を変更する必要があります。

+0

参考になります私は '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(); 
関連する問題