如何在导航视图中的导航标题中添加按钮?

时间:2017-02-13 12:26:04

标签: android navigation-drawer

我在主要活动中创建了导航抽屉。我在导航布局的导航标题中添加了一个开关。我为导航标题创建了一个单独的布局。我不知道如何为其添加功能如何执行任何功能都可以打开和关闭开关。

  <?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>

以下代码用于导航标题布局。我在导航标题中添加了开关和textview.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>

2 个答案:

答案 0 :(得分:2)

试试这段代码:

 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
    }       

});

答案 1 :(得分:1)

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

并使用'switch'作为普通开关。希望这就是你要找的东西