【问题标题】:How to set Navigation Drawer icons to the right side of the Item Texts?如何将导航抽屉图标设置到项目文本的右侧?
【发布时间】:2017-11-02 09:19:08
【问题描述】:

我的项目截图:My Project

我的目标设计:My Target

我创建了一个(从右到左)导航抽屉。它工作正常,但是当我试图将图标从左侧移动到右侧时,它不起作用。

<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" />

注意:我的 API 是 14+

【问题讨论】:

标签: android navigation-drawer drawerlayout


【解决方案1】:

我不确定 ActionBar 是否提供这样的功能。但是你可以通过使用一个菜单项来做到这一点。 在最右边创建一个菜单:

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

然后 OnmenuItem 点击打开/关闭抽屉:-

 @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】:

    在 setContentView(R.layout.main_activity); 之前添加到您的 Activity:

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

    这只能在 API 17+ 上实现

    【讨论】:

      【解决方案3】:

      需要更改代码中的以下内容。

      @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);
          }
      }
      

      在

      AndroidManifiest.xml

      android:supportsRtl="true"
      

      注意:这适用于 API 16 及更高版本。

      【讨论】:

        【解决方案4】:

        如果您的目标 API 为 17 岁以上,请使用此打开您的导航抽屉右侧

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

        试试这个创建一个新的布局,如下所示

         <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" />
        

        使用以下代码将自定义视图设置为您的导航视图

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

        setActionView(int resId)

        为此菜单项设置操作视图。

        编辑

        试试这个来修改你的单个导航菜单项

        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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-07
          • 2017-03-19
          相关资源
          最近更新 更多