我想自定义 android.support.design.widget.NavigationView 类以使 NavigationItems 从右到左,以便导航项图标出现在文本的右侧。
使用的支持库版本:
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:design:23.1.1'
这是用于 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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
    android:id="@+id/app_bar_main"
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<com.myapp.support.CustomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/colorPrimaryDark"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:itemIconTint="@color/drawer_item_tint"
    app:itemTextColor="@color/drawer_item_text"
    app:menu="@menu/activity_main_drawer">
</com.myapp.support.CustomNavigationView>
</android.support.v4.widget.DrawerLayout>
这是我从支持库中的 NavigationView 类继承的 CustomNavigationView 类:
package com.myapp.support;
import android.content.Context;
import android.os.Build;
import android.support.design.widget.NavigationView;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
public class CustomNavigationView extends NavigationView{
    public CustomNavigationView(Context context) {
        this(context, null);
    }
    public CustomNavigationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public CustomNavigationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //for api levels more than 17
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            ViewCompat.setLayoutDirection(this, ViewCompat.LAYOUT_DIRECTION_RTL);
    }
}
我使用了 ViewCompat.setLayoutDirection 方法;但它仅适用于 API 级别 17 及更高级别。我没有找到任何其他方法或属性,例如 LayoutParams。那么低层应该怎么做呢?有没有其他方法可以强制图标出现在文本的右侧?