drawerlayout配合NavigationView使用,实现侧滑菜单
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.zjl.application.MainActivity">
<LinearLayout
android:layout_width="match_parent"//用于替换的主布局
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主页面"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"//设置权重为左侧
android:fitsSystemWindows="true"
app:headerLayout="@layout/header_layout"//设置头布局
app:menu="@menu/main">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
/ /头布局
<?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="200dp"
android:orientation="vertical">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
//mainActivity代码
public class MainActivity extends AppCompatActivity {
private NavigationView viewById;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewById = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawre);
viewById.setItemIconTintList(null);//让图片显示自己的颜色
View headerView = viewById.getHeaderView(0);
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "这是头部局", Toast.LENGTH_SHORT).show();
}
});
viewById.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
switch (itemId){
case R.id.item1:
Toast.makeText(MainActivity.this, "选中第1个", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(Gravity.LEFT);
//在此可以用fragment开启事物替换主布局的内容
break;
case R.id.item2:
Toast.makeText(MainActivity.this, "选中第2个", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(Gravity.LEFT);
break;
case R.id.item3:
Toast.makeText(MainActivity.this, "选中第3个", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(Gravity.LEFT);
break;
case R.id.item4:
Toast.makeText(MainActivity.this, "选中第4个", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(Gravity.LEFT);
break;
case R.id.item5:
Toast.makeText(MainActivity.this, "选中第5个", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawer(Gravity.LEFT);
break;
}
return true;
}
});
}
}