项目经常会有这种需求,就是想动态的在某个界面上添加一个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()) {
@Override
public 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);
}
分享到:
相关推荐
不过至少算是框架类的代码。 代码如下:package arui; import android.app.Activity; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.view.Gravity; import...
android抽屉菜单 android panel 随便写的demo http://blog.csdn.net/zabio/article/details/22812903
在 .net C#,在panel上动态增加控件示例子,简单,一看就会.
Delphi 动态创建panel,在panel上再创建TRzBackground,显示背景,创建TBmpButton,有鼠标移动事件和单击事件
一个基于C#实现对 Microsoft Outlook动态添加panel的程序例子。
C# WinForm窗体控件Panel修改边框颜色以及边框宽度方法 可以直接到博客地址查看主要代码:http://www.cnblogs.com/JiYF/p/9047559.html
C# DataGridView单元格中动态添加多个控件。开发环境VS2012。
winform panel 绘制边框阴影 ,可以设置具体显示哪个边框的阴影。 static Image shadowDownRight = new Bitmap(typeof(ShadowPanel), "Images.tshadowdownright.png");//下右 static Image shadowDown = new ...
实现动态添加自定义控制到Panel,并可控制每行添加个数——实现自动换行
真的流畅,滑动的手速再快也赶不上代码刷新view的速度快。 高效。仅仅四个卡片view轻松搞定任意多的数据。 灵活。自定义ViewGroup对卡片view的高度实现了自适应。 细节。卡片之间联动的视觉效果,是像素级的精确。 ...
C# WinForm 在Panel中添加Form 教程+源码 可以像MDI窗体那个进行操作,用于解决界面美化过程中的MDI窗体, 新式弹窗.学习一下,绝对有用 C# winform必学
通过自定义扩展VirtualizingPanel,结合ListBox控件,实现了界面布局的灵活配置,支持1*2,1*3,2*2等任何布局方式,并且支持滚动条
提供了一个C#中主窗体Panel中加载其他多个窗体Panel控件的办法
sliding-panel A ViewGroup that implements a sliding panel that is part of the view hierarchy, not above it. Difference from other libraries All other implementations of the bottom sheet pattern ...
博客配套代码,针对jQuery Easy UI 面板布局的例子
实现功能: 动态添加自定义控件至Panel,每行添加个数可控,达到指定个数后自动进行换行; 其中包含只能输入字母或者数字以及退格键(Backspace)的自定义控件
Panel控件的使用 我们对控件进行分组的原因不外乎三个: 1、为了获得清晰的用户...动态添加控件一般需要经过下面三个步骤: 1、创建要添加的控件实例 2、设置新控件的属性。 3、将控件添加到父控件的 Controls 集合
Easyui的panel面板,在panel的面板中动态添加Linkbuton按钮的编辑方法,下面把实现思路及代码记录到脚本之家平台,供大家参考
Dynamic space measurement code based on matlab software