使我的导航抽屉标题可点击(使图像视图可点击)

时间:2019-06-08 21:09:44

标签: android header navigation

我的代码有问题,应用程序无法执行可点击的图像,并且在点击时不会显示Toast消息

我已经尝试过此链接,但是没有用 OnClick for navigation drawer header not working

这是我的主要活动:

    NavigationView navigationView = (NavigationView) 
    findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

     View headerview = navigationView.getHeaderView(0);
     ImageView imageButton = (ImageView) 
     headerview.findViewById(R.id.login);

    LinearLayout header = (LinearLayout) 
    headerview.findViewById(R.id.header);
     header.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Toast.makeText(getApplicationContext(),"Hello 
     Javatpoint",Toast.LENGTH_SHORT).show();
         }
     });

这是我的标头xml:

 <?xml version="1.0" encoding="utf-8"?>
 <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="@dimen/nav_header_height"
android:background="@color/splashBackground"
android:gravity="bottom"
android:id="@+id/header"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"

android:theme="@style/ThemeOverlay.AppCompat.Dark">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="75dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="15dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@drawable/realprolinklogo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:text="REAL PROLINK"
        android:textAppearance="@style/TextAppearance"
        android:textColor="@color/splashText"
        android:textSize="13dp"
        android:textStyle="bold" />




</LinearLayout>


<ImageView
    android:id="@+id/login"
    android:layout_width="125dp"
    android:layout_height="35dp"
    android:layout_marginLeft="62dp"
    app:srcCompat="@drawable/loginsignup"/>




  </LinearLayout>

这是我的导航视图:

 <?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/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:openDrawer="start">

  <include
    layout="@layout/app_bar_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  <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:headerLayout="@layout/nav_header_main2"
    app:menu="@menu/activity_main2_drawer" />

    </android.support.v4.widget.DrawerLayout>

没有错误消息

但是没有达到预期的结果

1 个答案:

答案 0 :(得分:0)

好的,因此,不要将标题布局包含在nav_view中,而是尝试以下操作:

 <?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/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:openDrawer="start">

  <LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@color/splashBackground"
android:gravity="bottom"
android:id="@+id/header"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="75dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="15dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@drawable/realprolinklogo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"

        android:text="REAL PROLINK"
        android:textAppearance="@style/TextAppearance"
        android:textColor="@color/splashText"
        android:textSize="13dp"
        android:textStyle="bold" />




</LinearLayout>


<ImageView
    android:id="@+id/login"
    android:layout_width="125dp"
    android:layout_height="35dp"
    android:layout_marginLeft="62dp"
    app:srcCompat="@drawable/loginsignup"/>




  </LinearLayout>

  <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:headerLayout="@layout/nav_header_main2"
    app:menu="@menu/activity_main2_drawer" />

    </android.support.v4.widget.DrawerLayout>