在前一篇文章自定义菜单样式中有提到怎样自定义菜单,那个方法在2.3版本之前好像都可以,2.3开始就不起作用了。具体原因不知道,可能是android对LayoutInflater.Factory进行了限制
2.3之后要实现自定义的菜单,只能自己实现了,下面是我参考别人的代码写的,出处忘了。
public class DancerMenu { private LinearLayout mLayout = null; private Context mContext = null; private Activity mActivity = null; private Animation mShowAnimation = null; private Animation mHideAnimation = null; private Resources res; private LinearLayout mWeather, mWallpaper, mNotification, mTask, mWidget; public DancerMenu(Context context,View view){ mContext = context; mLayout = (LinearLayout)(view.findViewById(R.id.layout_custom_menu)); ... } private class MenuOnClickListener implements OnClickListener{ public void onClick(View v) { switch(v.getId()){ ... break; default:break; } } } public void CreateMenu() { if (mLayout.getVisibility() == View.GONE) showAppMenu(); else hideAppMenu(); } private void showAppMenu(){ if(mShowAnimation == null){ mShowAnimation = AnimationUtils.loadAnimation(mContext, R.anim.menushow); } mLayout.startAnimation(mShowAnimation); mLayout.requestFocus(); mLayout.setVisibility(View.VISIBLE); } public void hideAppMenu(){ if(mHideAnimation == null) mHideAnimation = AnimationUtils.loadAnimation(mContext, R.anim.menuhide); mLayout.startAnimation(mHideAnimation); mLayout.setVisibility(View.GONE); } public boolean isShow(){ if(mLayout.getVisibility() == View.VISIBLE) return true; return false; } }
菜单呼出和隐藏的动画:
menuhide.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="140" android:duration="500" android:fillAfter="true" android:fillEnabled="true" /> </set>menushow.xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="140" android:toYDelta="0" android:duration="500" android:fillAfter="true" android:fillEnabled="true" /> </set>