NavigationView: custom item layout with app: actionLayout how?

according to the latest post by Yana Leika

And if you haven’t taken a look at NavigationView recently, there’s quite an update in version 23.1.0 [1] with the addition of app: actionLayout support to support custom views.

How to use app:actionLayoutin NavigationView?

Using

<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_drawer_item_1"
        android:icon="@drawable/ic_menu_1"
        android:title="@string/navigation_drawer_item_1"
        app:actionLayout="@layout/menu_test_1"
        />
    <item
        android:id="@+id/navigation_drawer_item_2"
        android:icon="@drawable/ic_menu_2"
        android:title="@string/navigation_drawer_item_2"
        app:actionLayout="@layout/menu_test_1"
        />
</group>

from

<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/nav_header_main"
    app:menu="@menu/activity_main_drawer">

This does not work. It shows the Layout action on the right side of the element. In addition, removal android:iconalso android:titleseems to be inoperative.

How can i fix this?

Thanks. Relations

+4
source share
1 answer

I beat this problem by removing any text from android: title.

NavigationView:

<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/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

(activity_main_drawer):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <group android:checkableBehavior="single">
        <item android:id="@+id/nav_item1"
            app:showAsAction="always"
            android:title=""
            app:actionLayout="@layout/menu_item_layout" />
    </group>
</menu>

menu_item_layout:

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

    <TextView android:id="@+id/item_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="Item text" />
</RelativeLayout>
+4

Source: https://habr.com/ru/post/1615713/


All Articles