安卓实现 汉堡抽屉 带输入框+抽屉内自定义布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    tools:context=".MainActivity">

    <!-- 主内容区域 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/edit_text"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="Search..." />

            </LinearLayout>
        </androidx.appcompat.widget.Toolbar>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- 主内容在这里填充 -->
        </FrameLayout>

    </LinearLayout>

    <!-- 抽屉菜单 -->
    <com.google.android.material.navigation.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/drawer_header"
        app:menu="@menu/drawer_menu" />


</androidx.drawerlayout.widget.DrawerLayout>
<!-- drawer_header.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <ImageView
        android:id="@+id/image_avatar"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:src="@drawable/ic_launcher_background"
        android:scaleType="centerCrop"
        android:layout_gravity="center_horizontal"/>

    <TextView
        android:id="@+id/text_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="John Doe"
        android:textSize="18sp"
        android:gravity="center"
        android:paddingTop="8dp"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_home"
        android:icon="@drawable/ic_launcher_background"
        android:title="Home" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_launcher_background"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_launcher_background"
        android:title="Slideshow" />
</menu>

package com.xmkjsoft.new_plan;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawerLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerLayout = findViewById(R.id.drawer_layout);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(menuItem -> {
            int itemId = menuItem.getItemId();
            if (itemId == R.id.nav_home) {
                // 处理 Home 菜单点击
            } else if (itemId == R.id.nav_gallery) {
                // 处理 Gallery 菜单点击
            } else if (itemId == R.id.nav_slideshow) {
                // 处理 Slideshow 菜单点击
            }
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        });


        EditText editText = findViewById(R.id.edit_text);



        View headerView = navigationView.getHeaderView(0); // 获取自定义布局

        ImageView avatarImageView = headerView.findViewById(R.id.image_avatar);
        TextView usernameTextView = headerView.findViewById(R.id.text_username);

// 设置头像和用户名的点击事件监听器等

    }

    @Override
    public void onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

相关推荐

  1. 定义ScrollView

    2024-05-25 23:16:09       34 阅读
  2. Flutter定义TextInputFormatter实现金额输入

    2024-05-25 23:16:09       53 阅读
  3. flutter 打包定义名字

    2024-05-25 23:16:09       34 阅读
  4. React实现抽屉组件

    2024-05-25 23:16:09       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 23:16:09       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 23:16:09       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 23:16:09       82 阅读
  4. Python语言-面向对象

    2024-05-25 23:16:09       91 阅读

热门阅读

  1. 使用Python从网站API下载视频并转换为MP4文件

    2024-05-25 23:16:09       34 阅读
  2. Pytorch-03 数据集与数据加载器

    2024-05-25 23:16:09       28 阅读
  3. 机器学习 - 模型训练

    2024-05-25 23:16:09       37 阅读
  4. 初说Stable Diffusion

    2024-05-25 23:16:09       34 阅读
  5. python list 重复元素不会覆盖

    2024-05-25 23:16:09       30 阅读
  6. 书籍推荐计算机相关

    2024-05-25 23:16:09       28 阅读
  7. websocket的压缩和wireshark如何解码tls

    2024-05-25 23:16:09       37 阅读
  8. k8s笔记 | helm包管理

    2024-05-25 23:16:09       38 阅读