Android进阶篇-自定义Menu(设置Menu的背景及文字属性)
2012-05-10 16:17
260 查看
系统自带的Menu有各种限制条件,如何设置Menu的背景和文字的各项属性呢?在不自定义的情况下,也是可以设置Menu的背景的。
但是设置Menu显示文字的各项属性就比较麻烦了,为了更好的解决这些问题,我们最好还是采用自定义Menu的方法。
自定义Menu就是一个自定义的PopWindow:
而相应的在主界面,我们也应该进行Menu拦截操作。
popup_animation.xml:
popup_enter.xml:
/** 设置Menu的背景图 */ protected void setMenuBackground() { this.getLayoutInflater().setFactory( new android.view.LayoutInflater.Factory() { public View onCreateView(String name, Context context,AttributeSet attrs) { // 指定自定义inflate的对象 if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) { try { LayoutInflater f = getLayoutInflater(); final View view = f.createView(name, null,attrs); new Handler().post(new Runnable() { public void run() { // 设置背景图片 view.setBackgroundResource(R.color.menu); } }); return view; } catch (InflateException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return null; } } ); }
但是设置Menu显示文字的各项属性就比较麻烦了,为了更好的解决这些问题,我们最好还是采用自定义Menu的方法。
自定义Menu就是一个自定义的PopWindow:
public class TabMenu extends PopupWindow{ private LinearLayout mLayout; private ImageView mImageView; private TextView mTextView; /** * @param context 上下文 * @param onClickListener 单击事件 * @param resID 图片资源 * @param text 显示的文字 * @param fontSize 显示的文字大小 * @param fontColor 文字的颜色 * @param colorBgTabMenu 背景颜色 * @param aniTabMenu 消失的动画 * @return */ public TabMenu(Context context,OnClickListener onClickListener,int resID,String text,int fontSize, int fontColor,int colorBgTabMenu,int aniTabMenu){ super(context); mLayout=new LinearLayout(context); mLayout.setOrientation(LinearLayout.VERTICAL); mLayout.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL); mLayout.setPadding(10, 10, 10, 10); mTextView = new TextView(context); mTextView.setTextSize((context.getResources().getDimensionPixelSize(fontSize))); mTextView.setTextColor((context.getResources().getColor(fontColor))); mTextView.setText(text); mTextView.setGravity(Gravity.CENTER); mTextView.setPadding(5, 5, 5, 5); mImageView=new ImageView(context); mImageView.setBackgroundResource(resID); mLayout.addView(mImageView,new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT))); mLayout.addView(mTextView); mLayout.setOnClickListener(onClickListener); this.setContentView(mLayout); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setBackgroundDrawable(new ColorDrawable(context.getResources().getColor(colorBgTabMenu))); this.setAnimationStyle(aniTabMenu); this.setFocusable(true); } }
而相应的在主界面,我们也应该进行Menu拦截操作。
private TabMenu tabMenu; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tabMenu = new TabMenu(this, null, R.drawable.ic_launcher, "设置", R.dimen.middle_text_size,R.color.blue,R.color.white,R.style.PopupAnimation); } /** 创建MENU */ public boolean onCreateOptionsMenu(Menu menu) { menu.add("menu");// 必须创建一项 return super.onCreateOptionsMenu(menu); } /** 拦截MENU */ public boolean onMenuOpened(int featureId, Menu menu) { if (tabMenu != null) { if (tabMenu.isShowing()) tabMenu.dismiss(); else { tabMenu.showAtLocation(findViewById(R.id.LinearLayout01), Gravity.BOTTOM, 0, 0); } } return false;// 返回为true 则显示系统menu }
popup_animation.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="PopupAnimation" parent="android:Animation"> <item name="android:windowEnterAnimation">@anim/popup_enter</item> <item name="android:windowExitAnimation">@anim/popup_exit</item> </style> </resources>
popup_enter.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="1000" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="1000" /> </set>
相关文章推荐
- Android进阶篇-自定义Menu(设置Menu的背景及文字属性)
- 【Android细节(一)】Gallery设置自定义的背景属性
- Android自定义属性为应用程序设置全局背景
- Android在代码中调用XML中设置的自定义View属性
- android在学习——Menu背景图片,背景色的设置
- Android自定义Spinner下拉框,可以设置提示文字
- Android TextView文字透明度和背景透明度设置
- Android-设置控件背景及文字颜色
- Android 自定义设置文字大小
- android TextView 设置部分文字背景色和文字颜色
- Android自定义View 自定义xml属性设置
- 自定义Android TabHost的背景及文字
- android spinner自定义背景文字显示不全解决办法
- Android 自定义toast总结:纯文本,带图像,带标题栏及自定义背景文字颜色
- Android自定义Menu背景
- Android开发之在xml中设置自定义属性
- android的自定义listview以及listview常用属性设置
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- android 自定义Dialog背景透明及显示位置设置
- android 自定义Dialog背景透明及显示位置设置