1.创建带侧滑效果的activity
右击,new---->activity---->选择NavgationDrawer Activity
2.xml文件布局
(1)activity_main_function.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.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"android:fitsSystemWindows="true"tools:openDrawer="start"><includelayout="@layout/app_bar_main_function"android:layout_width="match_parent"android:layout_height="match_parent" /><android.support.design.widget.NavigationViewandroid:id="@+id/nav_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="start"android:fitsSystemWindows="true"app:headerLayout="@layout/nav_header_main_function"app:menu="@menu/activity_main_function_drawer" /></android.support.v4.widget.DrawerLayout>
可以看到我们的最外层是DrawerLayout,包含了两个内容:include为显示内容区域,NavigationView为侧边抽屉栏。
NavigationView有两个app属性,分别为app:headerLayout和app:menu,headerLayout用于显示头部的布局(可选),menu用于建立MenuItem选项的菜单。
<?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"android:layout_width="match_parent"android:layout_height="@dimen/nav_header_height"android:background="@drawable/side_nav_bar"android:gravity="bottom"android:orientation="vertical"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingBottom="@dimen/activity_vertical_margin"android:theme="@style/ThemeOverlay.AppCompat.Dark"><ImageViewandroid:id="@+id/imageView"android:layout_width="80dp"android:layout_height="80dp"android:contentDescription="@string/nav_header_desc"android:paddingTop="@dimen/nav_header_vertical_spacing"app:srcCompat="@mipmap/ic_account_circle_white_48dp" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="@dimen/nav_header_vertical_spacing"android:text="@string/nav_header_title"android:textAppearance="@style/TextAppearance.AppCompat.Body1" /><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/nav_header_subtitle" /></LinearLayout>
(3)activity_main_function_drawer.xml(menu目录下)
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"tools:showIn="navigation_view"><group android:checkableBehavior="single"><itemandroid:id="@+id/nav_me"android:title="我"android:icon="@mipmap/ic_mine_gray_24"/><itemandroid:id="@+id/nav_friend"android:title="好友"android:icon="@mipmap/ic_friends_gray_24"/><itemandroid:id="@+id/nav_notification"android:title="通知"android:icon="@mipmap/ic_notification_gray_24"/><itemandroid:id="@+id/nav_message"android:title="私信"android:icon="@mipmap/ic_messages_gray_24"/></group><group android:checkableBehavior="none"android:id="@+id/group_manage"><itemandroid:id="@+id/nav_manage"android:title="应用管理"android:icon="@mipmap/ic_app_management_gray_24"/></group><item android:title="Communicate"><menu><itemandroid:id="@+id/nav_share"android:icon="@drawable/ic_menu_share"android:title="分享" /><itemandroid:id="@+id/nav_send"android:icon="@drawable/ic_menu_send"android:title="发送" /></menu></item></menu>
3.java后台
package com.example.administrator.test66smartbeijing;import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.NavigationView; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.GravityCompat; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView;import com.example.administrator.test66smartbeijing.fragment.GovernmentFragment; import com.example.administrator.test66smartbeijing.fragment.HomePagerFragment; import com.example.administrator.test66smartbeijing.fragment.NewsCenterFragment; import com.example.administrator.test66smartbeijing.fragment.SettingFragment; import com.example.administrator.test66smartbeijing.fragment.SmartServiceFragment;import java.util.ArrayList; import java.util.List;public class MainFunctionActivity extends AppCompatActivityimplements NavigationView.OnNavigationItemSelectedListener {ViewPager viewPager;List<Fragment> fragments;BottomNavigationView bottomNavigationView;MenuItem menuItem;TextView tv_main_title;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main_function);//设置顶部菜单栏Toolbar toolbar = findViewById(R.id.toolbar);setSupportActionBar(toolbar);DrawerLayout drawer =findViewById(R.id.drawer_layout);ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);drawer.addDrawerListener(toggle);toggle.syncState();NavigationView navigationView = findViewById(R.id.nav_view);navigationView.setNavigationItemSelectedListener(this);initBottomNavigationView();}@Overridepublic void onBackPressed() {DrawerLayout drawer = findViewById(R.id.drawer_layout);if (drawer.isDrawerOpen(GravityCompat.START)) {drawer.closeDrawer(GravityCompat.START);} else {super.onBackPressed();}}@SuppressWarnings("StatementWithEmptyBody")@Overridepublic boolean onNavigationItemSelected(MenuItem item) {// Handle navigation view item clicks here.int id = item.getItemId();if (id == R.id.nav_me) {// Handle the camera action} else if (id == R.id.nav_friend) {} else if (id == R.id.nav_notification) {} else if (id == R.id.nav_message) {} else if (id == R.id.nav_share) {} else if (id == R.id.nav_send) {}DrawerLayout drawer = findViewById(R.id.drawer_layout);drawer.closeDrawer(GravityCompat.START);return true;}private class Adapter extends FragmentPagerAdapter {List<Fragment> list1;public Adapter(FragmentManager fm, List<Fragment> fragments) {super(fm);this.list1=fragments;}@Overridepublic Fragment getItem(int position) {return list1.get(position);}@Overridepublic int getCount() {return list1.size();}} }
4.效果图
参考文献:https://www.cnblogs.com/JohnTsai/p/5172056.html