【问题标题】:How to change the icons direction for menu items in a NavigationView?如何更改 NavigationView 中菜单项的图标方向?
【发布时间】:2015-06-28 19:08:15
【问题描述】:

您好,我正在使用 Android NavigationView。我想将图标方向更改为 RTL 而不是 LTR,如下所示:

我该怎么做?

【问题讨论】:

标签: android material-design navigationview


【解决方案1】:

您可以像我一样在导航视图中使用列表视图。这样做的好处是您可以更好地控制:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/header"
    >
    <ListView
        android:id="@+id/navigationmenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="192dp"
        android:background="@android:color/white">
    </ListView>

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

导航项应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="@color/cardview_dark_background"
    >
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"
        android:src="@drawable/viewlist"
        />
    <TextView
        android:id="@+id/tvlstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_marginRight="30dp"
        android:textColor="@color/colorPrimary"

        />
</RelativeLayout>

和像这样的 navcustomadapter:

package com.example.android.dezcook;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Android on 6/4/2016.
 */
public class navCustomAdapter extends BaseAdapter {

    String[] result;
    Context context;
    int[] imageId;
    private static LayoutInflater inflater=null;
    public navCustomAdapter(MainActivity mainActivity,String[] prgmNameList,int[] prgImages)
    {
        result=prgmNameList;
        imageId=prgImages;
        context =mainActivity;
        inflater =(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    }
    @Override
    public int getCount()
    {
        return result.length;
    }
    @Override
    public Object getItem(int position)
    {
        return position;
    }
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    public class Holder
    {
        TextView tv;
        ImageView img;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        Holder holder=new Holder();
        View rowView;
        rowView=inflater.inflate(R.layout.navitem,null);
        holder.tv=(TextView)rowView.findViewById(R.id.tvlstName);
        holder.img=(ImageView)rowView.findViewById(R.id.img);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);
        rowView.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View v) {
                                           Toast.makeText(context,"you Clicked "+result[position],Toast.LENGTH_LONG).show();
                                       }
                                   }


        );
        return rowView;
    }

}

最后在主活动中这样调用它:

 ListView lv=(ListView)findViewById(R.id.navigationmenu);
        lv.setAdapter(new navCustomAdapter(this,navItem,prgmImages)

【讨论】:

    【解决方案2】:

    想办法!

    尝试将这些属性添加到布局文件中的NavigationView

     android:layoutDirection="rtl"
     android:textDirection="rtl"
    

    截图

    注意:它似乎适用于 API 17 及更高版本

    【讨论】:

      【解决方案3】:

      我遇到了这个问题。我的应用是“ltr”,我想在“rtf”模式下查看抽屉。 我的解决方案: 在导航视图中我清除了:

      • app:menu="@menu/drawer_menu"
      • app:headerLayout="@layout/drawer_nav_header"

      并替换:

      • 包括 layout="@layout/drawer_nav_header"/>
      • 包括 layout="@layout/ly_custom_menu_item"/>

        <include layout="@layout/fragment_map" />
        <FrameLayout
            android:id="@+id/content_frame"
            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="right"
            android:background="@color/white"
            android:fitsSystemWindows="true">
        
            <!--app:menu="@menu/drawer_menu"
            app:headerLayout="@layout/drawer_nav_header"-->
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
        
                <include layout="@layout/drawer_nav_header"/>
                <include layout="@layout/ly_custom_menu_item"/>
        
             </LinearLayout>
        
        </android.support.design.widget.NavigationView>
        

      ly_custom_menu_item.xml:

       <?xml version="1.0" encoding="utf-8"?>
       <ScrollView
          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:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/linearLayout"
          android:background="@color/white"
          android:orientation="vertical">
      
      <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
      
      <LinearLayout
              android:id="@+id/linearItemAnalysis"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="@dimen/picSize_50"
              android:orientation="horizontal"
              android:gravity="center_vertical|right"
              android:paddingRight="@dimen/margin_dp_8"
              android:foreground="?selectableItemBackgroundBorderless">
      
          <TextView
                  android:text="navAnalysis"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginRight="@dimen/margin_dp_16"/>
      
          <android.support.v7.widget.AppCompatImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/ic_analysis_menu"
                  app:layout_constraintEnd_toEndOf="parent"/>
      </LinearLayout>
      
      <LinearLayout
              android:id="@+id/linearItemRouting"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="@dimen/picSize_50"
              android:orientation="horizontal"
              android:gravity="center_vertical|right"
              android:paddingRight="@dimen/margin_dp_8"
              android:foreground="?selectableItemBackgroundBorderless">
      
          <TextView
                  android:text="routing"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginRight="@dimen/margin_dp_16"/>
      
          <android.support.v7.widget.AppCompatImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/ic_routing"
                  app:layout_constraintEnd_toEndOf="parent"/>
      </LinearLayout>
      <LinearLayout
              android:id="@+id/linearItemAddress"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:minHeight="@dimen/picSize_50"
              android:orientation="horizontal"
              android:gravity="center_vertical|right"
              android:paddingRight="@dimen/margin_dp_8"
              android:foreground="?selectableItemBackgroundBorderless">
      
          <TextView
                  android:text="navAddress"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginRight="@dimen/margin_dp_16"/>
      
          <android.support.v7.widget.AppCompatImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/ic_address"
                  app:layout_constraintEnd_toEndOf="parent"/>
         </LinearLayout>
      
         </LinearLayout>
      
       </ScrollView>
      

      enter image description here

      【讨论】:

        【解决方案4】:

        尝试设置您的导航抽屉项目布局重力。示例:

        android:gravity="right"
        

        【讨论】:

          猜你喜欢
          • 2015-11-17
          • 1970-01-01
          • 2015-10-10
          • 2019-03-22
          • 1970-01-01
          • 1970-01-01
          • 2015-09-19
          • 2015-12-13
          • 2018-09-19
          相关资源
          最近更新 更多