4

我需要將切換到導航抽屜中的項目內。我正在使用新的設計支持庫,但我無法找到它是否屬於posibble。使用時切換進入安卓設計支持庫的導航抽屜項目

android:checkable 

項目只是完整的選擇,這不是我所希望的。

這是我真正想要的截圖。這有可能實現嗎?

enter image description here

+0

你好@mlody,我想問。如何在導航抽屜中添加該開關。我在這裏問了一個問題= http://stackoverflow.com/questions/33951901/android-design-navigation-drawer-how-to-add-a-switch-in-nav-xml - 謝謝 – Joolah

+0

@Joolah不幸的是我沒有添加這個開關,因爲我沒有找到方法來做到這一點,沒有外部NavigationDrawer。 –

回答

11

您的抽屜式導航欄菜單項:

<item 
    android:id="@+id/nav_item1" 
    android:icon="@drawable/ic_item1" 
    android:title="item1" 
    app:actionLayout="@layout/layout_switch" 
    /> 

和該項目的佈局:

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

    <android.support.v7.widget.SwitchCompat 
     android:id="@+id/drawer_switch" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:text=""/> 

</LinearLayout> 
+2

非常感謝。有用。我很遺憾,一年前我不知道。 –

+0

如何爲交換機添加監聽器,是否可以將代碼添加到您的響應中 – mehmet

+1

如何向交換機添加監聽器,因爲您顯然無法以正常方式進行操作。 – mogren3000

0

您應該可以。

你的抽屜式導航欄視圖可以是LinearLayout了,裏面的是,你可以包括你的NavigationView和另一ViewGroup添加開關。

+0

你的意思是把放在標籤之間嗎?我可以做到這一點,但我得到的只是文本視圖和切換繪製導航抽屜ImageView不作爲它的一部分,但在它上面。 –

1

根本是你可以做到這一點很容易,我會後我的代碼,這樣你就可以實現它

<item android:id="@+id/nav_item_1" 
     android:title="Android" 
     android:icon="@drawable/ic_android_black_24dp" 
     android:checked="true"/> 

和主要活動佈局應該是:

<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" 
tools:context=".MainActivity"> 

<LinearLayout 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/colorPrimary" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> 

    <TextView 
     android:id="@+id/content_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="50dp" 
     android:text="Your Content Goes Here" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/colorAccent" /> 

    <!-- your content layout --> 

</LinearLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/menu_drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:headerLayout="@layout/drawer_header" 
    app:menu="@menu/menu_drawer" /> 

而這裏的一步一步tutorial for Navigation Drawer using Design Library

+0

解決方案中沒有開關。 –

9

我發現這樣做的一種方法是使用setActionView的菜單項你想要的:

mNavigationView.getMenu().findItem(R.id.nav_connect) 
     .setActionView(new Switch(this)); 

// To set whether switch is on/off use: 
((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true); 

可能需要一個點擊監聽,以及改變開關的狀態:我還沒有嘗試

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { 
    @Override 
    public boolean onNavigationItemSelected(MenuItem menuItem) { 

     switch (menuItem.getItemId()) { 
      case R.id.nav_connect: 
       ((Switch) menuItem.getActionView()).toggle(); 
       return true; 
     } 
    } 
} 

,但你很可能在xml中使用android:actionLayout =「@ layout/switch_layout」並指向您創建的自定義佈局。

也可以嘗試使用ActionProvider,它可能會提供更強大一點的功能。但我還沒有嘗試過這種方法。

2

我做了這樣的事情。

navigationView.getMenu().findItem(R.id.vibrate) 
      .setActionView(new Switch(this)); 

    Switch vibrateSwitch =(Switch) 
navigationView.getMenu().findItem(R.id.vibrate).getActionView(); 
    vibrateSwitch.setChecked(true); 
    vibrateSwitch.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener(){ 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean 
isChecked){ 
       SharedPreferences sharedPreferences = 
getSharedPreferences(getString(R.string.MyPREFERENCES), MODE_PRIVATE); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putBoolean(getString(R.string.VIBRATE), isChecked); 
       editor.commit(); 
     } 

    });