项目经常会有这种需求,就是想动态的在某个界面上添加一个Panel。比如,有一个按钮,点击后会显示下拉菜单式的界面。这种需求,就属于动态添加一个Panel。需求多了,就要研究是否可以抽象出通用的框架代码,以方便开发,所以就有了以下内容。

这里说是框架,说的大了点,其实没有那么复杂,只是一个容易扩展的基类而已。不过至少算是框架类的代码。

package arui;import android.app.Activity;import android.os.Handler;import android.os.Looper;import android.os.Message;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.view.ViewManager;import android.widget.FrameLayout;/** * Base class for panel. *  */public abstract class BasePanel {/** * left up position */public static final int LEFT_UP = 1;/** * right up position */public static final int RIGHT_UP = 2;/** * left bottom position */public static final int LEFT_BOTTOM = 3;/** * right bottom position */public static final int RIGHT_BOTTOM = 4;private static final int DEFAULT_MARGIN = 10;private static final int SHOW_PANEL = 0;private Activity activity;private LayoutParams parameters;private View view = null;private int layoutId;/** * constructor. *  * @param activity *            this panel will be attached to the activity * @param layoutId *            the panel's layout id */public BasePanel(Activity activity, int layoutId) {this.activity = activity;this.layoutId = layoutId;}/** * The developer can use this method to add the panel to the Activity. *  * @param act *            Activity * @param params *            LayoutParams */public void attach(LayoutParams params) {parameters = params;mHandler.sendMessage(mHandler.obtainMessage(SHOW_PANEL));}/** * The developer can use this method to add the panel to the Activity. *  * @param act *            Activity * @param position *            int. You can use BasePanel.LEFT_UP,BasePanel.RIGHT_UP, *            BasePanel.RIGHT_BOTTOM or BasePanel.LEFT_BOTTOM. */public void attach(int position) {attach(position, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN,DEFAULT_MARGIN);}/** * The developer can use this method to add the panel to the Activity. *  * @param act *            Activity * @param position *            int. You can use BasePanel.LEFT_UP,BasePanel.RIGHT_UP, *            BasePanel.RIGHT_BOTTOM or BasePanel.LEFT_BOTTOM. * @param leftMargin *            int, left margin. * @param topMargin *            int, top margin. * @param rightMargin *            int, right margin. * @param bottomMargin *            int, bottom margin. *  */public void attach(int position, int leftMargin, int topMargin,int rightMargin, int bottomMargin) {FrameLayout.LayoutParams params = null;params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);params.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);switch (position) {case LEFT_UP:params.gravity = Gravity.LEFT;break;case RIGHT_UP:params.gravity = Gravity.RIGHT;break;case LEFT_BOTTOM:params.gravity = Gravity.LEFT | Gravity.BOTTOM;break;case RIGHT_BOTTOM:params.gravity = Gravity.RIGHT | Gravity.BOTTOM;break;default:break;}attach(params);}/** * The developer can use this method to remove the panel from the Activity. *  */public void remove() {if (view != null) {ViewManager mViewManager = (ViewManager) view.getParent();if (mViewManager != null) {mViewManager.removeView(view);}}}private Handler mHandler = new Handler(Looper.getMainLooper()) {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case SHOW_PANEL:if (view == null) {LayoutInflater factory = LayoutInflater.from(activity);view = factory.inflate(layoutId, null);}dealwithPanel(view);remove();activity.addContentView(view, parameters);break;}}};/** * do something with this panel. *  * @param view *            View of the panel */public abstract void dealwithPanel(View view);}


更多相关文章

  1. 粗看 Android(安卓)4.0 界面变化
  2. 2011年Android人才奇缺30万
  3. 【Android】Xposed 框架解析
  4. Android(安卓)Studio上方便使用butterknife注解框架的偷懒插件An
  5. 请问一下android程序员面试的时候怎么介绍android app的项目经历
  6. 【Android】 Tablayout、XTablayout、SlidingTablayout的比较
  7. Android目前流行三方数据库ORM分析及对比
  8. android开发初试(基于GUI的开发)
  9. 基于Android的 拍照上传 程序开发

随机推荐

  1. 冷月手撕408之操作系统(12)-内存的分配与
  2. 冷月手撕408之数据结构(4)-链表
  3. 冷月手撕408之操作系统(21)-文件的实现
  4. 冷月手撕408之操作系统(4)-操作系统的运
  5. 冷月手撕408之操作系统(21)-文件的共享与
  6. 冷月手撕408之操作系统(12)-内存管理基础
  7. 冷月手撕408之操作系统(3)-操作系统的发
  8. 冷月手撕408之操作系统(2)-操作系统的基
  9. PHP变量的知识点
  10. Laravel 添加自定义函数