Подтвердить что ты не робот

Как добавить пользовательские представления в новый NavigationView

Я пытаюсь добавить переключатель как menuitem в NavigationView, как этот

enter image description here

Я использовал атрибут actionViewClass, но он отображает только заголовок.

<item
android:id="@+id/navi_item_create_notifications_sound"
android:title="Notifications Sounds"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:actionViewClass="android.support.v7.widget.SwitchCompat"
app:showAsAction="always" />
4b9b3361

Ответ 1

Новая библиотека поддержки 23.1

позволяет использовать пользовательский вид для элементов в навигационном представлении, используя app:actionLayout или MenuItemCompat.setActionView().

enter image description here

Вот как мне удалось отобразить SwitchCompat

menu_nav.xml

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

    <group
        android:id="@+id/first"
        android:checkableBehavior="single">

        <item
            android:id="@+id/navi_item_1"
            android:icon="@drawable/ic_feed_grey_500_24dp"
            android:title="Feed" />
        <item
            android:id="@+id/navi_item_2"
            android:icon="@drawable/ic_explore_grey_500_24dp"
            android:title="Explore" />
        <item
            android:id="@+id/navi_item_4"
            android:icon="@drawable/ic_settings_grey_500_24dp"
            android:title="Settings" />

    </group>
    <group
        android:id="@+id/second"
        android:checkableBehavior="single">
        <item xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/navi_item_create_notifications_sound"
            android:title="Notifications Sounds"
            app:actionLayout="@layout/menu_swich"
            app:showAsAction="always" />

    </group>
</menu>

menu_switch.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="right|center_vertical"
    app:buttonTint="@color/colorPrimary"
    app:switchPadding="@dimen/spacing_small" />

Чтобы получить представление и назначить ему события, необходимо выполнить следующие действия:

SwitchCompat item = (SwitchCompat) navigationView.getMenu().getItem(3).getActionView();
        item.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener(){
            @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Logr.v(LOG_TAG, "onCheckedChanged" + isChecked);
            }
        });

Ответ 2

Простое решение при использовании 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:menu="@menu/activity_main_drawer">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/mSwitch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Night Mode" />

    </LinearLayout>

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

Ответ 3

Попробуйте включить ваш коммутатор в отдельный файл макета:

Меню

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/menu_switch"
    android:title="Switch Title"
    app:actionLayout="@layout/layout_my_switch"
    app:showAsAction="always" />
</menu>

Переключатель: "layout_my_switch.xml"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.SwitchCompat
    android:id="@+id/my_switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />
</RelativeLayout>

Ответ 4

[Обновить 03-03-2017] Ответ устарел. Не ссылайтесь на это. См. Принятый ответ.

К сожалению, в настоящее время NavigationView мало чем отличается от настройки...

Вы должны перенести Customized ListView в NavigationView.

<android.support.design.widget.NavigationView
    android:id="@+id/navView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

И создайте ячейку для этого списка, переместив TextView влево и SwitchCompact в правую сторону.

Надеюсь, это поможет вам...

Ответ 5

Я использовал макет в макете Drawer, где был использован код навигационного представления.

<android.support.design.widget.NavigationView
        android:id="@+id/navi_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start|top"
        android:background="@color/navigation_view_bg_color"
        app:theme="@style/NavDrawerTextStyle">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/drawer_header" />

            <include layout="@layout/navigation_drawer_menu" />
        </LinearLayout>
    </android.support.design.widget.NavigationView>

Ответ 6

Вы ищете что-то вроде этого,

Затем в Synfusion имеется пользовательский компонент, называемый Ящик навигации.

Вы можете найти соответствующую документацию здесь.

Человек.! они также предлагают лицензию сообщества.