使用android设计支持库将onClickListener添加到导航视图标题

使用android设计支持库将onClickListener添加到导航视图标题,android,androiddesignsupport,navigationview,Android,Androiddesignsupport,Navigationview,我想在导航视图标题的项目中添加onClickListener,如更改TextView文本或为ImageView设置图像。在activity\u main.xml布局中添加导航抽屉代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_pa

我想在导航视图标题的项目中添加onClickListener,如更改TextView文本或为ImageView设置图像。

在activity\u main.xml布局中添加导航抽屉代码

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

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

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"></FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        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:layout_gravity="start"
        android:layout_marginTop="-24dp"
        app:headerLayout="@layout/drawer_header"
        app:itemTextColor="@color/black"
        app:menu="@menu/nav_menu"/>

</android.support.v4.widget.DrawerLayout>
然后在onCreate()中初始化它们:

更新:27/10/2015

随着支持库的更新,在xml中向导航视图添加标题不起作用,这可能是一个错误

添加标题视图的步骤 在java中将视图膨胀为导航视图标题

mNavigationView.inflateHeaderView(R.layout.layout_header_profile);
希望这会有帮助。 快乐编码…

你可以做到这一点-

View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
或者,如果您有多个标题:

navigationView.getHeaderCount();

在视图上以XML格式设置onClickListener,如下所示:

android:onClick="onLabelClick"
然后在你的活动中

public void onLabelClick(View view) {
    // do stuff :)
}

这个问题可能会从一些代码中受益,例如,显示您到目前为止已经尝试了什么。我还没有尝试过这部分的任何代码,我想要一些类似gmail应用程序在导航视图标题中所做的事情。人们不会给出这样的代码。首先你自己试试。谢谢我添加了导航视图,我只需要header的处理程序,thanks任何方法都要注意,从support appcompat library v23.1获取headerView引用的方法是调用
navigationView.getHeaderView(0)
(如果你只有一个header)
mNavigationView.inflateHeaderView(R.layout.layout_header_profile);
View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.textView);
navigationView.getHeaderCount();
android:onClick="onLabelClick"
public void onLabelClick(View view) {
    // do stuff :)
}