代码如下:
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;
public abstract class BasePanel {
public static final int LEFT_UP = 1;
public static final int RIGHT_UP = 2;
public static final int LEFT_BOTTOM = 3;
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;
public BasePanel(Activity activity, int layoutId) {
this.activity = activity;
this.layoutId = layoutId;
}
public void attach(LayoutParams params) {
parameters = params;
mHandler.sendMessage(mHandler.obtainMessage(SHOW_PANEL));
}
public void attach(int position) {
attach(position, DEFAULT_MARGIN, DEFAULT_MARGIN, DEFAULT_MARGIN,
DEFAULT_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);
}
publ ic 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;
}
}
};
public abstract void dealwithPanel(View view);
}
您可能感兴趣的文章:Android使用addView动态添加组件的方法android ListView内数据的动态添加与删除实例代码Android动态添加menu菜单的简单方法Android 动态添加Fragment的实例代码Android用RecyclerView实现动态添加本地图片Android动态添加view的方法示例Android编程实现长按Button按钮连续响应功能示例Android实现圆角Button按钮Android开发中button按钮的使用及动态添加组件方法示例