繁体   English   中英

如何向材质NavigationDrawer标题添加动作按钮

[英]How to add action button to Material NavigationDrawer header

我需要在导航抽屉的标题中添加操作按钮。 但是我找不到任何好的解决方案。 我需要在我的应用程序中实现以下结果:

当您单击该按钮时,您应该看到类似以下内容:

不幸的是,我不知道如何将该按钮添加到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="liz@gmail.com"
            />

</LinearLayout>

是否有人知道如何轻松添加该按钮?

谢谢

我认为:您应该将此XML添加到您的res-main

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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM