在Android中动态添加Panel框架的实现代码
程序员文章站
2023-11-23 22:56:40
这里说是框架,说的大了点,其实没有那么复杂,只是一个容易扩展的基类而已。不过至少算是框架类的代码。复制代码 代码如下:package arui; import...
这里说是框架,说的大了点,其实没有那么复杂,只是一个容易扩展的基类而已。不过至少算是框架类的代码。
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.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);
}