2017-02-13 182 views
0

我在主activity中创建了导航抽屉。我在导航布局的导航标题中添加了一个开关。我为导航标题创建了单独的布局。我不知道如何为开关添加功能。如何开关任何功能。如何在导航视图中的导航页眉中添加按钮?

<?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/activity_dash_board" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.seyali.calllogs.DashBoardActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 
     <include 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      layout="@layout/toolbar_layout" 
      > 

     </include> 
    </LinearLayout> 


    <android.support.design.widget.NavigationView 

     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:id="@+id/navigation_View" 
     android:layout_gravity="start" 
     app:menu = "@menu/drawer_menu" 
     app:headerLayout="@layout/navigation_drawer_header"> 
    </android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout> 

在下面的代码是用于导航标题layout.I在导航header.navigation_drawer_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:background="@drawable/backgroung_red_colour"> 
    <Switch 
     android:text="Call Recording" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/navigation_drawer_switch" 
     android:layout_alignParentBottom="true" 
     android:gravity="left" 
     android:textOff="off" 
     android:textOn="on"/> 

    <TextView 
     android:text="Switch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/navigation_drawer_switch" 
     android:layout_alignParentStart="true" 
     android:layout_marginBottom="28dp" 
     android:id="@+id/drawertext" /> 
</RelativeLayout> 
+0

交换机是问题或抽屉? –

+0

没有问题。我不知道如何初始化和执行开关的功能。 –

+0

如何初始化开关并执行特定功能? –

回答

1

试试这个代码:

NavigationView navigationView = (NavigationView) findViewById(R.id.your_nav_view_id); 
View header = navigationView.getHeaderView(0); 

Switch switch_view = (Switch)header.findViewById(R.id.navigation_drawer_switch); 
switch_view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    Log.v("Switch State=", ""+isChecked); 
    if(isChecked) 
     //switch is on 
    else 
     //switch is off 
    }  

}); 
+0

谢谢@ rafsanahmad007 ....它的工作原理... –

1
View header = navigationView.getHeaderView(0); 
Switch switch = (Switch)navigationView..findViewById(R.id.navigation_drawer_switch); 

添加开关和所述的TextView并使用“开关”作为正常开关。希望这是你在寻找什么