简介drawerlayout
- drawerlayout大概就是抽屉的意思
- Support Library包中实现了侧滑菜单效果的控件
- drawerLayout分为侧边菜单和主内容区两部分
- drawerLayout其实是一个布局控件,跟LinearLayout等控件是一种东西,但是drawerLayout带有滑动的功能。只要按照drawerLayout的规定布局方式写完布局,就能有侧滑的效果
drawerlayout用法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?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"> <FrameLayout android:id="@+id/main_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView android: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_container" app:menu="@menu/activity_course_table_drawer" /> </android.support.v4.widget.DrawerLayout>
|
-
**在drawerlayout中放置两个控件,第一个控件是主内容区域,第二个控件是侧滑栏的内容
-
drawerLayout左侧菜单(或者右侧)的展开与隐藏可以被DrawerLayout.DrawerListener的实现监听到
-
这样就可以在侧滑菜单展开或者关闭的时候做一些处理了
-
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| mDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close ) { public void onDrawerClosed(View view) { getActionBar().setTitle(mTitle); invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { getActionBar().setTitle(mDrawerTitle); invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle);
|
侧滑菜单
- 侧滑菜单是一个普通的View
- 可以自定义
- 这里我使用了Navigation
关于侧滑菜单的展开与隐藏
- 使用了DrawerLayout的openDrawer方法 和 closeDrawer方法
Navigation
1 2 3 4 5 6 7 8 9
| <android.support.design.widget.NavigationView android:id="@+id/id_nv_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left" android:fitsSystemWindows="true" app:headerLayout="@layout/header_just_username" app:menu="@menu/menu_drawer" />
|
app:headerLayout="@layout/header_just_username"
app:menu="@menu/menu_drawer"
- **这两行用于配置headerlayout和menu
- 其中headerlayout是自定义的头部
- menu则是一个菜单
- 在activity或者fragment中通过配置菜单的方式来配置Navigation
- 示例:
1 2 3
| navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); course_Item = navigationView.getMenu().getItem(0);
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| @Override public boolean onNavigationItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.item1: parameterToFragment.putString("id",id); courseTableFragment = new CourseTableFragment(); courseTableFragment.setArguments(parameterToFragment); getFragmentManager().beginTransaction() .replace(R.id.main_container , courseTableFragment) .commit(); break; case R.id.item2: parameterToFragment.putString("id",id); gradeFragment = new GradeFragment(); gradeFragment.setArguments(parameterToFragment); getFragmentManager().beginTransaction() .replace(R.id.main_container , gradeFragment) .commit(); break; case R.id.item3: mathFragment = new MathFragment(); getFragmentManager().beginTransaction() .replace(R.id.main_container , mathFragment) .commit(); break; case R.id.item4: Intent intent4 = new Intent(MainContainerActivity.this , OtherActivity.class); intent4.putExtra("id" , id); startActivity(intent4); break; case R.id.item8: parameterToFragment.putString("id",id); Intent intent_9 = new Intent(MainContainerActivity.this,SettingActivity.class); startActivity(intent_9); break; } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true; }
|