Android進階篇-自定義Menu(設置Menu的背景及文字屬性)

系統自帶的Menu有各種限制條件,如何設置Menu的背景和文字的各項屬性呢?在不自定義的情況下,也是可以設置Menu的背景的。

	/** 設置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="100%p" android:toYDelta="0" android:duration="1000" />  
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" /> 
</set>
popup_exit.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>







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章