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 のような他のメソッドや属性は見つかりませんでした。じゃあ低レベルはどうすればいいの?テキストの右側にアイコンを強制的に表示する他の方法はありますか?