xml文件
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawerlayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ui.ht.rscja.com.testitem.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" /> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#AAAAAA"/> <FrameLayout android:background="#66FF66" android:id="@+id/frame_content" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/navigationView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="left" android:fitsSystemWindows="true" app:headerLayout="@layout/fragment_head" app:menu="@menu/left_menu"/> </android.support.v4.widget.DrawerLayout>
MainActivity.java//上面的anctionbar private void setBar(){ setSupportActionBar(toolbar);//注意将主题设置为NoActionBar getSupportActionBar().setDisplayHomeAsUpEnabled(true); actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close); actionBarDrawerToggle.syncState();//← 变成 == drawerLayout.setDrawerListener(actionBarDrawerToggle); drawerLayout.setStatusBarBackgroundColor(ContextCompat.getColor(this,R.color.colorBackgrund)); }// 创建Actionbar menu 菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu,menu); return true; } //处理menu菜单里面的点击事件 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.menu_look: break; case R.id.menu_set: break; } return true;//return super.onOptionsItemSelected(item); }
本文介绍了一种使用Android的DrawerLayout实现侧滑菜单的方法,包括XML布局配置及MainActivity中的ActionBar设置过程。通过该方法可以轻松地在应用中加入侧滑菜单功能。
1962

被折叠的 条评论
为什么被折叠?



