編輯:關於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 ListmItems = 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
用到的布局文件:
用到的背景
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(); } });
幾乎每個APP都會用的相機功能,下面小編把內容整理分享到本站平台,供大家參考,感興趣的朋友一起學習吧!啟動相機的兩種方式1.直接啟動系統相機<code class=
一、TabHost用法通常情況下我們會通過繼承TabActivity,調用getTabHost()獲取TabHost實例,下面是具體過程。TabHostActivity.
一、效果圖二、描述更改Android項目中的語言,這個作用於只用於此APP,不會作用於整個系統三、解決方案(一)布局文件<LinearLayout xmlns:an
原文地址:http://android.xsoftlab.net/training/multiscreen/index.html引言Android運行於數以百計不同尺寸的