Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自定義主菜單

Android 自定義主菜單

編輯:關於Android編程

本文介紹一個超簡單的自定義主菜單,效果如下:

\

原理:其實就是對原生的Dialog的一個簡單的封裝。並加上顯示和隱藏的動畫效果。再給控件加上回調事件。

 

TestDialog.java

 

public class TestDialog implements OnClickListener {
	private Context mContext;
	private Dialog mDialog;
	private Display mDisplay;
	private TextView mTitle;
	private TextView mOk, mCancel;
	private LinearLayout mContent;
	private List mItems = null;
	private List mListeners = null;
	private List mDrawables = null;

	public TestDialog(Context context) {
		this.mContext = context;
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		mDisplay = windowManager.getDefaultDisplay();
	}

	/**
	 * builder()創建
	 * 
	 * @return
	 */
	public TestDialog builder(int layout) {
		initView(layout);
		return this;
	}

	/**
	 * 初始化Dialog視圖的方法
	 * 
	 * @param layout
	 */
	private void initView(int layout) {
		View view = LayoutInflater.from(mContext).inflate(layout, null);
		view.setMinimumWidth(mDisplay.getWidth());
		//
		mTitle = (TextView) view.findViewById(R.id.menu_title);
		mContent = (LinearLayout) view.findViewById(R.id.menu_content);
		mOk = (TextView) view.findViewById(R.id.menu_ok);
		mCancel = (TextView) view.findViewById(R.id.menu_cancel);
		mOk.setOnClickListener(this);
		mCancel.setOnClickListener(this);
		mDialog = new Dialog(mContext, R.style.DialogStyle);
		mDialog.setContentView(view);
		Window dialogWindow = mDialog.getWindow();
		dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);
	}

	/**
	 * 
	 * @param 是否能取消
	 * @return
	 */
	public TestDialog setCancelable(boolean b) {
		if (mDialog != null) {
			mDialog.setCancelable(b);
		}
		return this;
	}

	/**
	 * 設置對話框標題
	 * 
	 * @param title
	 * @return
	 */
	public TestDialog setTitle(String title) {
		if (mDialog != null) {
			mTitle.setText(title);
			mTitle.setVisibility(View.VISIBLE);
		}
		return this;
	}

	/**
	 * 
	 * @param 點擊Dialog以外的部分是否可以取消
	 * @return
	 */

	public TestDialog set(boolean b) {
		if (mDialog != null) {
			mDialog.setCanceledOnTouchOutside(b);
		}
		return this;
	}

	/**
	 * 取消
	 */
	public void dismiss() {
		mDialog.dismiss();
	}

	/**
	 * 用來裝視圖數據的集合
	 * 
	 * @param title
	 * @return
	 */
	public TestDialog addItem(String title, Drawable drawable, OnCustomItemClickListener clickListener) {
		if (mItems == null) {
			mItems = new ArrayList();
		}
		if (mListeners == null) {
			mListeners = new ArrayList();
		}
		if (mDrawables == null) {
			mDrawables = new ArrayList();
		}
		mItems.add(title);
		mListeners.add(clickListener);
		mDrawables.add(drawable);
		return this;
	}

	private void setItem() {
		if (mItems == null || mItems.size() <= 0) {
			return;
		}
		int count = mItems.size();
		for (int i = 0; i < count; i++) {
			final int index = i;
			TextView view = new TextView(mContext);
			view.setText(object + 1);
			view.setCompoundDrawablesRelativeWithIntrinsicBounds(null, mDrawables.get(i), null, null);
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					mListeners.get(index).onClick(index);
				}
			});
			mContent.addView(view);
		}
	}

	public void show() {
		setItem();
		mDialog.show();
	}

	public interface OnCustomItemClickListener {
		public abstract void onClick(int v);
	}

	@Override
	public void onClick(View v) {
		if (v == mOk) {

		} else if (v == mCancel) {

		}
		mDialog.dismiss();

	}

}

 

用到的Teme:

 

動畫文件:

 

dialog_in.xml

 


dialog_out.xml

 

 





 

用到的布局文件:

 




    

    
    

    

    

        

        

        
    

 

 

用到的背景

 



    
    
    



在Activity中使用:

 

 

findViewById(R.id.click).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TestDialog dialog = new TestDialog(MainActivity.this);
				dialog.builder(R.layout.menu);
				dialog.setCancelable(false);
				dialog.addItem(微博1, getResources().getDrawable(R.drawable.ic_weibo), new OnCustomItemClickListener() {

					@Override
					public void onClick(int v) {
						Toast.makeText(getBaseContext(), 1, 1).show();
					}
				});
				dialog.addItem(微博2, getResources().getDrawable(R.drawable.ic_weibo), new OnCustomItemClickListener() {

					@Override
					public void onClick(int v) {
						Toast.makeText(getBaseContext(), 3, 1).show();
					}
				});
				dialog.show();
			}
		});



 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved