本实践在于练习使用ActionBarDrawerToggle+DrawerLayout实现双向侧滑栏菜单布局
需要的库文件 android-v7-appcompat
所选要的jar android-support-v4.jar,android.jar
效果图
点击Home菜单 ↓-->searchview被隐藏
点击照相机 ↓-->searchview被隐藏
点击搜索 ↓
点击隐藏菜单 ↓
首先是ActionBar的菜单设计toggle_menu.xml
<menu 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" tools:context="com.example.actiontabbar.DrawerToggleActivity" > <item android:id="@+id/action_search" android:orderInCategory="100" android:title="@string/action_search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="ifRoom" /> <item android:id="@+id/action_camera" android:orderInCategory="100" android:icon="@drawable/ofm_camera_icon" android:title="@string/action_camera" app:showAsAction="ifRoom"/> <item android:id="@+id/action_settings" android:orderInCategory="100" android:icon="@drawable/ofm_setting_icon" android:title="@string/action_settings" app:showAsAction="never"/> </menu>
布局文件drawertoggle_layout.xml
<?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/main_drawer_layout" android:orientation="vertical" > <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/main_left_menu" android:layout_width="200dip" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="start" android:background="#f09001" /> <LinearLayout android:id="@+id/main_right_menu" android:layout_width="200dip" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="end" android:background="#f09001" /> </android.support.v4.widget.DrawerLayout>
Activity代码
package com.example.actiontabbar; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import android.content.res.Configuration; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewConfiguration; import android.view.Window; import android.widget.LinearLayout; public class DrawerToggleActivity extends ActionBarActivity{ private DrawerLayout mDrawerLayout; private LinearLayout leftMenuPanel; private LinearLayout rightMenuPanel; private ActionBarDrawerToggle actionBarDrawerToggle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); forceShowOverflowMenu(); setContentView(R.layout.drawertoggle_layout); initDrawerLayout(); } private void initDrawerLayout() { mDrawerLayout = (DrawerLayout)findViewById(R.id.main_drawer_layout); leftMenuPanel = (LinearLayout) findViewById(R.id.main_left_menu); rightMenuPanel = (LinearLayout) findViewById(R.id.main_right_menu); mDrawerLayout.setScrimColor(0x000000); mDrawerLayout.setDrawerShadow(R.drawable.goss_whitemask_drawable,GravityCompat.START); actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.abc_ic_menu_moreoverflow_normal_holo_dark, R.string.action_toggle,R.string.action_toggle){ @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { //actionbar上的home icon //END即gravity.right 从右向左显示 START即left 从左向右弹出显示 mDrawerLayout.closeDrawer(rightMenuPanel);//关闭抽屉 if (mDrawerLayout.isDrawerVisible(leftMenuPanel)) { mDrawerLayout.closeDrawer(leftMenuPanel);//关闭抽屉 } else { mDrawerLayout.openDrawer(leftMenuPanel);//打开抽屉 } return true; } else if(item.getItemId()==R.id.action_camera) { mDrawerLayout.closeDrawer(leftMenuPanel);//关闭抽屉 //END即gravity.right 从右向左显示 START即left 从左向右弹出显示 if (mDrawerLayout.isDrawerVisible(rightMenuPanel)) { mDrawerLayout.closeDrawer(rightMenuPanel);//关闭抽屉 } else { mDrawerLayout.openDrawer(rightMenuPanel);//打开抽屉 } return true; } return super.onOptionsItemSelected(item); } @Override public void onDrawerClosed(View drawerView) { if(drawerView==leftMenuPanel) { super.onDrawerClosed(drawerView); } getSupportActionBar().setTitle(R.string.app_name); postInvalidateOptionsMenu(); } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if(drawerView==rightMenuPanel) { super.onDrawerClosed(drawerView); } getSupportActionBar().setTitle(R.string.action_toggle); postInvalidateOptionsMenu(); } @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); if(drawerView==rightMenuPanel) { super.onDrawerClosed(drawerView); } } @Override public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); } }; getSupportActionBar().setDisplayHomeAsUpEnabled(true);//给home icon的左边加上一个返回的图标 getSupportActionBar().setIcon(new ColorDrawable(0x00ffffff)); mDrawerLayout.setDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.setDrawerIndicatorEnabled(true); } private void postInvalidateOptionsMenu() { try { Method method = this.getClass().getMethod("invalidateOptionsMenu", new Class<?>[]{}); if(method!=null) { if (Modifier.isPublic(method.getModifiers()) && Modifier.isPublic(method.getDeclaringClass().getModifiers())) { method.invoke(this, new Object[]{}); } else if(!method.isAccessible()) { method.setAccessible(true); method.invoke(this, new Object[]{}); } } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } private void forceShowOverflowMenu() { try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class .getDeclaredField("sHasPermanentMenuKey"); if (menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { e.printStackTrace(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toggle_menu, menu); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav drawer is open, hide action items related to the content view boolean drawerOpen = mDrawerLayout.isDrawerOpen(leftMenuPanel) || mDrawerLayout.isDrawerOpen(rightMenuPanel); menu.findItem(R.id.action_search).setVisible(!drawerOpen);//search的显示与drawer的显示相反 return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (actionBarDrawerToggle.onOptionsItemSelected(item)) { return true; } return super.onOptionsItemSelected(item); } @Override public boolean onMenuOpened(int featureId, Menu menu) { if (featureId == Window.FEATURE_ACTION_BAR && menu != null) { if (menu.getClass().getSimpleName().equals("MenuBuilder")) { try { Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(menu, true); } catch (Exception e) { } } } return super.onMenuOpened(featureId, menu); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); actionBarDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); actionBarDrawerToggle.onConfigurationChanged(newConfig); } }