1.使用魅族的demo里的SmartBarUtils.java
2.在mainifest中的Application
android:theme="@android:style/Theme.DeviceDefault.Light"
android:uiOptions="splitActionBarWhenNarrow"
3.在基类的activity中:
//代表隐藏头部bar
SmartBarUtils.setActionBarViewCollapsable(getActionBar(), true);
getActionBar().setDisplayOptions(0);
4.在业务的fragment中:
必须先调用下面的代码,才能是fragment里的menu生效
setHasOptionsMenu(true);
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.home_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home_page:
new AlertDialog.Builder(mAct).setTitle("Action Menu Item")
.setPositiveButton("Yes", null).setNegativeButton("Cancel", null).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
魅族的反射工具类:
public class SmartBarUtils {
public static void setActionBarTabsShowAtBottom(ActionBar actionbar, boolean showAtBottom) {
try {
Method method = Class.forName("android.app.ActionBar").getMethod(
"setTabsShowAtBottom", new Class[] { boolean.class });
try {
method.invoke(actionbar, showAtBottom);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void setActionBarViewCollapsable(ActionBar actionbar, boolean collapsable) {
try {
Method method = Class.forName("android.app.ActionBar").getMethod(
"setActionBarViewCollapsable", new Class[] { boolean.class });
try {
method.invoke(actionbar, collapsable);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void setActionModeHeaderHidden(ActionBar actionbar, boolean hidden) {
try {
Method method = Class.forName("android.app.ActionBar").getMethod(
"setActionModeHeaderHidden", new Class[] { boolean.class });
try {
method.invoke(actionbar, hidden);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}