NavigationView
效果图
布局文件
// An highlighted block
<?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="bw.com.day3.Main3Activity"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/tb"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="@color/colorPrimary"
app:logo="@mipmap/ic_launcher_round"
app:title="标题"
app:titleTextColor="#ff0"
app:subtitle="副标题"
app:subtitleTextColor="#00f"
app:navigationIcon="@android:drawable/sym_action_call"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入要查询的内容"
android:drawableLeft="@android:drawable/ic_menu_search"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="@+id/dl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="主界面"
/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:orientation="vertical"
app:headerLayout="@layout/layout_haend"
app:menu="@menu/navigaationmenu"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Menu文件
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item android:id="@+id/item1" android:title="我的超级会员" android:icon="@drawable/chazhao"></item>
<item android:id="@+id/item2" android:title="我的超级会员" android:icon="@drawable/chazhao"></item>
<item android:id="@+id/item3" android:title="我的超级会员" android:icon="@drawable/chazhao"></item>
<item android:id="@+id/item4" android:title="我的超级会员" android:icon="@drawable/chazhao"></item>
</group>
</menu>
头文件
// An highlighted block
<?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="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/_104"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是头布局"
/>
</LinearLayout>
java代码
// An highlighted block
package bw.com.day3;
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.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
import bw.com.day1.R;
public class Main3Activity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
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.nv);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
if (menuItem.getItemId() == R.id.item1){
Toast.makeText(Main3Activity.this, "菜单1+", Toast.LENGTH_SHORT).show();
}
return false;
}
});
ImageView imageView = navigationView.getHeaderView(0).findViewById(R.id.imageview);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main3Activity.this, "头文件", Toast.LENGTH_SHORT).show();
}
});
setSupportActionBar(toolbar); // 让他来替换actionbar
initView();
initDarerLayout();
}
public void initView(){
toolbar = findViewById(R.id.tb);
//导航按钮添加点击事件
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main3Activity.this, "点击了导航", Toast.LENGTH_SHORT).show();
}
});
}
public void initDarerLayout(){
drawerLayout = findViewById(R.id.dl);
//绑定toolbar和drawerLayout
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(Main3Activity.this,drawerLayout,toolbar,R.string.openlayout,R.string.closelayout);
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu,menu);//填充菜单
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu1){
Toast.makeText(this, "一级菜单", Toast.LENGTH_SHORT).show();
}else if (item.getItemId() == R.id.subMenu){
Toast.makeText(this, "二级菜单", Toast.LENGTH_SHORT).show();
if (drawerLayout.isDrawerOpen(Gravity.LEFT)){//抽屉是否从左打开
drawerLayout.closeDrawer(Gravity.LEFT);
}else {
drawerLayout.openDrawer(Gravity.LEFT);
}
}
return super.onOptionsItemSelected(item);
}
}