侧拉布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/Drawer_Layout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//利用Toolbar时注意这里的陷阱 要关闭模拟器自带的导航栏
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:title="我是toolbar"
app:titleTextColor="#f00" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="我是内容页" />
</LinearLayout>
<!-- android:layout_gravity="top和bottom不能用 只能左右" ☆☆-->
<!--记得导依赖com.android.support:design (com.android.support:design:28.0.0)-->
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Navi_View"
android:layout_gravity="start"
app:headerLayout="@layout/activity_header"
app:menu="@menu/mian"
></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Toolbar 陷阱解决
方式一
方式二
直接在androidmanifest中的application或者activity加入
头部布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Head_Image"
android:src="@mipmap/ic_launcher_round"
android:layout_centerInParent="true"/>
</RelativeLayout>
menu布局
在这<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--orderInCategory这个是设置显示优先级的-->
<item android:id="@+id/item_One"
android:title="设置"/>
<item android:id="@+id/item_Two"
android:title="分享"/>
<item android:id="@+id/item_Three"
android:icon="@android:drawable/ic_menu_add"
android:title="添加"/>
<group android:id="@+id/group1" >
<item
android:id="@+id/item_Four"
android:title="睡觉觉"/>
<item
android:id="@+id/item_Five"
android:title="打LOL"/>
</group>
</menu>里插入代码片
大致效果如下
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private NavigationView Navi_View;
private ImageView mImage;
//很多api
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawtwo);
initView();
}
private void initView() {
mDrawerLayout = findViewById(R.id.Drawer_Layout);
Navi_View = (NavigationView) findViewById(R.id.Navi_View);
//从NavigationView里面获取头布局
View view = Navi_View.getHeaderView(0);
mImage = view.findViewById(R.id.Head_Image);
//头部点击事件
mImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "睡觉觉", Toast.LENGTH_SHORT).show();
}
});
//这个是菜单的点击事件
Navi_View.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_One:
Toast.makeText(MainActivity.this, "itemOne", Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawers();
break;
case R.id.item_Two:
Toast.makeText(MainActivity.this, "itemTwo", Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawers();
break;
case R.id.item_Three:
Toast.makeText(MainActivity.this, "itemThree", Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawers();
break;
}
return false;
}
});
}
}