2016-04-25 207 views
1

我在android應用程序中創建了導航抽屜,並且正確地從右側出現它,但是隻有菜單項對齊到右側時出現了一個問題,但是我不能,我該怎麼做?將導航菜單項對齊到android

在左側 enter image description here

這與菜單項的圖片顯示的導航是main_activity佈局代碼

<?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_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:openDrawer="start"> 

<include 
    layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 



<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="end" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

,這是activity_main_drawer菜單項的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

<group android:checkableBehavior="single" > 
    <item 
     android:id="@+id/nav_camera" 

     android:icon="@drawable/ic_menu_camera" 
     android:title="Camera" /> 
    <item 
     android:id="@+id/nav_gallery" 
     android:icon="@drawable/ic_menu_gallery" 
     android:title="Gallery" /> 
    <item 
     android:id="@+id/nav_slideshow" 
     android:icon="@drawable/ic_menu_slideshow" 
     android:title="Slideshow" /> 
    <item 
     android:id="@+id/nav_manage" 
     android:icon="@drawable/ic_menu_manage" 
     android:title="Tools" /> 
</group> 

<item android:title="Communicate"> 
    <menu> 
     <item 
      android:id="@+id/nav_share" 
      android:icon="@drawable/ic_menu_share" 
      android:title="Share" /> 
     <item 
      android:id="@+id/nav_send" 
      android:icon="@drawable/ic_menu_send" 
      android:title="Send" /> 
    </menu> 
</item> 

在這些項目中,我需要不是在左側,因爲我的導航從右側打開至左在右側顯示它的問題

MainActivity代碼:

public class ParentMainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

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

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "You have chosen mail option", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

    ImageButton menuRight = (ImageButton) findViewById(R.id.menuRight); 



    menuRight.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (drawer.isDrawerOpen(GravityCompat.END)) { 
       drawer.closeDrawer(GravityCompat.END); 
      } else { 
       drawer.openDrawer(GravityCompat.END); 
      } 
     } 
    }); 
    /*ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState();*/ 

    NavigationView navigationView1 = (NavigationView) findViewById(R.id.nav_view); 

    navigationView1.setNavigationItemSelectedListener(this); 

} 

@Override 
public void onBackPressed() { 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    if (drawer.isDrawerOpen(GravityCompat.END)) { 
     drawer.closeDrawer(GravityCompat.END); 
    } else { 
     super.onBackPressed(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    String text = ""; 
    if (id == R.id.nav_camera) { 
     text = "camera"; 
    } else if (id == R.id.nav_gallery) { 
     text = "gallery"; 
    } else if (id == R.id.nav_slideshow) { 
     text = "slideshow"; 
    } else if (id == R.id.nav_manage) { 
     text = "tools"; 
    } else if (id == R.id.nav_share) { 
     text = "share"; 
    } else if (id == R.id.nav_send) { 
     text = "send"; 

    Toast.makeText(this, "You have chosen " + text, Toast.LENGTH_LONG).show(); 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 

    drawer.closeDrawer(GravityCompat.END); 
    return true; 
} 

}

+0

發佈您的代碼。 –

+0

@MoralesBatovski好的,我添加了它。 –

回答

0

如果您的項目使用的API級別爲17或更高,則可以將android:supportsRtl添加到您的清單

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
... 
</application> 

此屬性聲明您的應用程序是否願意支持從右到左(RTL)佈局。如果設置爲true並且targetSdkVersion設置爲17或更高,則系統會激活並使用各種RTL API,以便您的應用可以顯示RTL佈局。如果設置爲false或者targetSdkVersion設置爲16或更低,則RTL API將被忽略或無效,並且無論與用戶的Locale選項關聯的佈局方向如何,您的應用程序都會表現相同(您的佈局將始終保留-to-右)。

編輯: 在這個post表明,存在一種強制RTL模式,如果它是可用的答案。在onCreate()方法調用forceRTLIfSupported()

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) 
private void forceRTLIfSupported() { 
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ 
     getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 
    } 
} 
+0

但是這取決於系統語言,所以我們沒有任何其他方法可以在不依賴系統語言的情況下將此菜單放在右側? 我試過你的解決方案,如果我改變我的系統語言,它工作得很好 –

+0

你可以強制RTL模式。檢查編輯答案! –

+0

完美答案非常感謝你:) –