2016-08-24 212 views
8

我的要求是下面的圖片我的導航抽屜應該從右側打開。我已經實現了這一點。我的導航抽屜從右向左打開。但問題是切換圖標總是在左側。如何將切換圖標設置在右側。我已經檢查了以下SO問題,但沒有一個人得到任何幫助。Android導航抽屜切換圖標右側

Change toggle button image Icon In Navigation Drawer right to left

Drawer Toggle in right Drawer

enter link description here

enter image description here

這是我都試過了。我的佈局

代碼activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="end"> 

    <android.support.design.widget.CoordinatorLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context="com.example.nav.MainActivity" 
     android:foregroundGravity="right"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="end" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       android:layout_gravity="right" 
       app:popupTheme="@style/AppTheme.PopupOverlay" 
       android:foregroundGravity="right" 
       android:textAlignment="viewEnd" 
       android:touchscreenBlocksFocus="false" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main" /> 

    </android.support.design.widget.CoordinatorLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_gravity="end" 
     app:headerLayout="@layout/nav_header" 
     app:menu="@menu/menu_navigation" 
     android:textAlignment="viewEnd" /> 


</android.support.v4.widget.DrawerLayout> 

代碼爲我的活動

public class MainActivity extends AppCompatActivity { 
    private DrawerLayout drawerLayout; 
    private Toolbar toolbar; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     initNavigationDrawer(); 

    } 

    @TargetApi(Build.VERSION_CODES.M) 
    public void initNavigationDrawer() { 

     NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view); 
     navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(MenuItem menuItem) { 

       int id = menuItem.getItemId(); 

       switch (id){ 
        case R.id.home: 
         Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show(); 
         drawerLayout.closeDrawers(); 
         break; 
        case R.id.settings: 
         Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_SHORT).show(); 
         break; 
        case R.id.trash: 
         Toast.makeText(getApplicationContext(),"Trash",Toast.LENGTH_SHORT).show(); 
         drawerLayout.closeDrawers(); 
         break; 
        case R.id.logout: 
         finish(); 

       } 
       return true; 
      } 
     }); 
     drawerLayout = (DrawerLayout)findViewById(R.id.drawer); 

     ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){ 

      @Override 
      public void onDrawerClosed(View v){ 
       super.onDrawerClosed(v); 
      } 

      @Override 
      public void onDrawerOpened(View v) { 
       super.onDrawerOpened(v); 
      } 

      @Override 
      public boolean onOptionsItemSelected(MenuItem item) { 
       if (item != null && item.getItemId() == android.R.id.home) { 
        if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) { 
         drawerLayout.closeDrawer(Gravity.RIGHT); 
        } 
        else { 
         drawerLayout.openDrawer(Gravity.RIGHT); 
        } 
       } 
       return false; 
      } 
     }; 
     drawerLayout.addDrawerListener(actionBarDrawerToggle); 
     actionBarDrawerToggle.syncState(); 

     toolbar.setNavigationOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) { 
        drawerLayout.closeDrawer(Gravity.RIGHT); 
       } else { 
        drawerLayout.openDrawer(Gravity.RIGHT); 
       } 
      } 
     }); 
    } 

} 

在此先感謝。

回答

20

我寫的EndDrawerToggle類非常相似,你的設置 - 一個DrawerLayout與終端對準抽屜View,在AppCompatActivity用自定義Toolbar爲支撐ActionBar

import android.app.Activity; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.graphics.drawable.DrawerArrowDrawable; 
import android.support.v7.widget.AppCompatImageButton; 
import android.support.v7.widget.Toolbar; 
import android.support.v7.widget.Toolbar.LayoutParams; 
import android.view.View; 
import android.view.View.OnClickListener; 


public class EndDrawerToggle implements DrawerLayout.DrawerListener { 

    private DrawerLayout drawerLayout; 
    private DrawerArrowDrawable arrowDrawable; 
    private AppCompatImageButton toggleButton; 
    private String openDrawerContentDesc; 
    private String closeDrawerContentDesc; 

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

     this.drawerLayout = drawerLayout; 
     this.openDrawerContentDesc = activity.getString(openDrawerContentDescRes); 
     this.closeDrawerContentDesc = activity.getString(closeDrawerContentDescRes); 

     arrowDrawable = new DrawerArrowDrawable(toolbar.getContext()); 
     arrowDrawable.setDirection(DrawerArrowDrawable.ARROW_DIRECTION_END); 

     toggleButton = new AppCompatImageButton(toolbar.getContext(), null, 
               R.attr.toolbarNavigationButtonStyle); 
     toolbar.addView(toggleButton, new LayoutParams(GravityCompat.END)); 
     toggleButton.setImageDrawable(arrowDrawable); 
     toggleButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        toggle(); 
       } 
      } 
     ); 
    } 

    public void syncState() { 
     if (drawerLayout.isDrawerOpen(GravityCompat.END)) { 
      setPosition(1f); 
     } 
     else { 
      setPosition(0f); 
     } 
    } 

    public void toggle() { 
     if (drawerLayout.isDrawerOpen(GravityCompat.END)) { 
      drawerLayout.closeDrawer(GravityCompat.END); 
     } 
     else { 
      drawerLayout.openDrawer(GravityCompat.END); 
     } 
    } 

    public void setPosition(float position) { 
     if (position == 1f) { 
      arrowDrawable.setVerticalMirror(true); 
      toggleButton.setContentDescription(closeDrawerContentDesc); 
     } 
     else if (position == 0f) { 
      arrowDrawable.setVerticalMirror(false); 
      toggleButton.setContentDescription(openDrawerContentDesc); 
     } 
     arrowDrawable.setProgress(position); 
    } 

    @Override 
    public void onDrawerSlide(View drawerView, float slideOffset) { 
     setPosition(Math.min(1f, Math.max(0, slideOffset))); 
    } 

    @Override 
    public void onDrawerOpened(View drawerView) { 
     setPosition(1f); 
    } 

    @Override 
    public void onDrawerClosed(View drawerView) { 
     setPosition(0f); 
    } 

    @Override 
    public void onDrawerStateChanged(int newState) { 
    } 
} 

EndDrawerToggle類是在這種情況下ActionBarDrawerToggle一個完成替換,所以你不需要任何當前已經擁有了設置的。所有DrawerListener方法仍然可用於替代,但對於基本功能而言,並不需要這樣做,因爲EndDrawerToggle可以自行切換抽屜狀態。同樣無需自己處理切換點擊,因此您不需要導航OnClickListener

只需實例化切換,將其添加爲DrawerListener,並將其同步。我建議同步onPostCreate()方法中的切換,以確保它在同步方向更改後正確同步。

private EndDrawerToggle drawerToggle; 
... 

public void initNavigationDrawer() { 
    NavigationView navigationView = ... 
    ... 

    drawerLayout = (DrawerLayout)findViewById(R.id.drawer); 

    drawerToggle = new EndDrawerToggle(this, 
             drawerLayout, 
             toolbar, 
             R.string.drawer_open, 
             R.string.drawer_close); 

    drawerLayout.addDrawerListener(drawerToggle); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    drawerToggle.syncState(); 
} 
+1

非常感謝,終於有效 –

+2

沒問題。樂意效勞。我應該提到,結束切換隻是「工具欄」的常規子項,因此如果您添加其他子視圖,或者需要下拉菜單,您可能需要手動執行以保持對齊正確。只是一個頭。乾杯! –

+3

我喜歡這個解決方案,絕對是拯救男人的絕對生活,對抗RTL,謝謝bro – Michael

-1

在您的Android清單加上此行:

android:supportsRtl="true" 

到您的應用程序,例如:在你的onCreate方法

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 

然後,加入這一行:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

警告:::這隻適用於SdkVersion 17+,所以如果你的應用程序的目標是較低的最低SDK,你將不得不crea定製菜單並覆蓋OnCreateOptions方法(除非有另一種我不知道的方式,這絕對是可能的)。

https://developer.android.com/guide/topics/manifest/application-element.html#supportsrtl

+1

感謝您的回答,從技術上說,您的解決方案改變了整個應用程序的方向。這不是我想要的 –

+1

對不起!對於我的應用程序,它只會改變菜單!很高興你找到了上面的正確解決方案! – LBJ33

+0

這不是一個正確的答案。默認情況下,您正在強制您的活動和整個應用使用RTL佈局。設置「View.LAYOUT_DIRECTION_RTL」將會佈置每個文本視圖和editext文件,以像阿拉伯語言那樣考慮右邊的開始。 – JaydeepW