一、效果图
二、代码示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher_background"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这是头布局"/>
</LinearLayout>
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.bw.day03.Main3Activity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffff"
app:title="标题"
app:subtitle="副标题"
app:navigationIcon="@mipmap/ic_launcher">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入你要搜索的内容"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="这是主页面"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="@layout/header"
app:menu="@menu/navigation_item">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
package com.bw.day03;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;
import day01.bw.com.myapplication.R;
public class Main3Activity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle toggle;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main3);
navigationView = findViewById(R.id.navigationView);
toolbar = findViewById(R.id.toolBar);
drawerLayout = findViewById(R.id.drawerLayout);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main3Activity.this, "导航按钮点击事件", Toast.LENGTH_SHORT).show();
}
});
toggle = new ActionBarDrawerToggle(Main3Activity.this,drawerLayout,toolbar,R.string.open_layout,R.string.close_layout);
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_item,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.menu1:
Log.e("###菜单","菜单一");
break;
case R.id.childmenu1:
Log.e("###菜单","子菜单一");
if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
drawerLayout.closeDrawer(Gravity.LEFT);
}else{
drawerLayout.openDrawer(Gravity.LEFT);
}
break;
case R.id.childmenu2:
Log.e("###菜单","子菜单二");
break;
case R.id.childmenu3:
Log.e("###菜单","子菜单三");
break;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="none">
<item android:id="@+id/supervip" android:icon="@mipmap/ic_launcher_round" android:title="我的超级会员"></item>
<item android:id="@+id/money" android:icon="@mipmap/ic_launcher_round" android:title="QQ钱包"></item>
<item android:id="@+id/decoration" android:icon="@mipmap/ic_launcher_round" android:title="个人装扮"></item>
<item android:id="@+id/mylove" android:icon="@mipmap/ic_launcher_round" android:title="我的收藏"></item>
</group>
</menu>