如何将动作按钮添加到Material NavigationDrawer标题

问题描述:

我需要将动作按钮添加到我的导航抽屉的标题。但我找不到任何好的解决方案。我需要在我的应用程序来实现这一结果:如何将动作按钮添加到Material NavigationDrawer标题

当你点击这个按钮,你应该看到这样的事情:

可惜的是,我不知道我应该如何添加按钮到我的XML。这里是我的抽屉完整的包头的.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="280dp" 
    android:layout_height="240dp" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/drawer_header_layout" 
     android:layout_width="280dp" 
     android:layout_height="168dp" 
     android:background="@drawable/placeholder" 
     android:gravity="bottom" 
     android:orientation="vertical" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

     <ImageView 
      android:id="@+id/drawer_header_image" 
      android:layout_width="64dp" 
      android:layout_height="64dp" 
      android:layout_marginBottom="8dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_marginTop="43dp" 
      android:contentDescription="null" 
      android:scaleType="centerCrop" 
      tools:src="@drawable/avatar_circle" /> 

     <TextView 
      android:id="@+id/drawer_header_name" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="3dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginStart="16dp" 
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
      android:textColor="@color/bodyTextColor" 
      android:textStyle="bold" 
      tools:text="Elizabeth Cray" /> 

     <TextView 
      android:id="@+id/drawer_header_email" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="9dp" 
      android:layout_marginLeft="16dp" 
      android:layout_marginStart="16dp" 
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
      tools:text="[email protected]" 
      /> 

</LinearLayout> 

是否有别人谁知道如何轻松地添加按钮?

感谢

+0

你应该能够用溢出图标添加另一个ImageView。但是,我从来没有在应用中看到UX模式。通常,您在NavigationView的列表中添加一个额外的操作 –

+0

我认为这是一个ImageButton,该图标作为源放置在布局中,该布局用作导航的标题。 – Aiapaec

在我看来:你应该将这个XML添加到您的资源,主要

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/miCompose" 
    android:icon="@drawable/male_profile" 
    app:showAsAction="ifRoom" 
    android:title="Your Image "> 
</item> 

</menu> 

你应该在MainActivity调用此为:

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 

    } 
+1

这不是添加到工具栏,而不是NavigationView? –

+0

是的,但我认为这可能会更容易和简单,可以帮助这篇文章.. – Niroj

+0

虽然我总体上同意,它并没有真正回答问题,它说*我需要添加动作按钮到我的导航抽屉的标题* –