How to set Navigation Drawer icons to the right side of the Item Texts?

妖精的绣舞 提交于 2019-12-20 05:23:31

问题


My Project Screenshot: My Project

My Target Design: My Target

I have created a (right to left) navigation drawer. It's working fine but when I'm trying to move the icon from the left side to the right side, it's not working.

<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="wrap_content"
    android:layout_height="wrap_content" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:background="@color/nav"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemIconTint="#d2a967"
    app:itemTextColor="#d2a967"
    app:menu="@menu/activity_main_drawer" />

Attention: My API is 14+


回答1:


I am not sure if ActionBar provide such feature . But you can do it by just using a menu item . Create a Menu on extreme right:

 <item
    android:id="@+id/menu_action_Drawer"
    android:layout_width="wrap_content"
    app:actionLayout="@layout/item_menu_drawer"
    android:title=""
    app:showAsAction="always"/>

Then OnmenuItem click Open/Close the Drawer:-

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_action_Drawer:
            if (drawerLayout.isDrawerOpen(GravityCompat.END)) {
                drawerLayout.closeDrawer(GravityCompat.END);
            } else {
                drawerLayout.openDrawer(GravityCompat.END);
            }
            break;
    }
}



回答2:


Add to your Activity before setContentView(R.layout.main_activity);:

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }

This is only possible on API 17+




回答3:


use this open your Navigation drawer right side if your target API is 17+

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
  }

Try this Create a new Layout like below

 <TextView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/tv_badge"
       android:layout_width="wrap_content"
       android:drawableRight="@mipmap/ic_launcher_round"
       android:layout_height="wrap_content" />

than set Custom view to Your navigation view using below code

 YourNavigationView.getMenu().getItem(0).setActionView(R.layout.custom_layout);

setActionView(int resId)

Set an action view for this menu item.

EDIT

try this to modify your single navigation menu item

TextView  textView = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
textView.setText("ONE");
textView.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_favourite, 0, 0);
textView.setTextColor(ContextCompat.getColor(this,R.color.colorPrimary));
navigationView.getMenu().getItem(0).setActionView(textView);



回答4:


Need to change following in your code.

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) //call this before super.onCreate
private void forceRtlIfSupported() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
    }
}

In

AndroidManifiest.xml

android:supportsRtl="true"

NOTE : This will work from API 16 and above version.



来源:https://stackoverflow.com/questions/47071568/how-to-set-navigation-drawer-icons-to-the-right-side-of-the-item-texts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!