在Android中动态添加Panel框架的实现代码
发布时间:2022-01-17 23:17:07
标签:android,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);
}


猜你喜欢
- 这周在做公司的一个C#项目中,要写一个webservice提供一个下载方法,之前公司有过,但是要整改,于是这种鸟屎摊子又交给了我,其中一个密
- 这篇文章主要介绍了java多线程加锁以及Condition类的使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 引入依赖<dependency> <groupId>org.apache.tika</groupI
- 看了下网上代码:我想要的效果如下图下划线和文字有15dp的间距 eeeeee的颜色上代码,<"1.0
- 一、使用方式可以采用Transactional,配置propagation即可。打开org.springframework.transact
- 协程与并发Kotlin协程是基于线程执行的。经过一层封装以后,Kotlin协程面对并发,处理方式与Java不同。在java的世界里,并发往往
- 本文实例讲述了Android编程实现AIDL(跨进程通信)的方法。分享给大家供大家参考,具体如下:一. 概述:跨进程通信(AIDL),主要实
- 作者:冰封一夏出处:http://www.cnblogs.com/bfyx/HZHControls官网:http://www.hzhcont
- app_main上一篇文章:# Android 10 启动分析之servicemanager篇 (二)在init篇中有提到,init进程会在
- android studio 版本不同连接手机方式有细微的不同,主要方式相似。介绍主要分手机和电脑两部分介绍。一、手机部分1、手机端下载一个
- 以下弹出框是框的实现,放入到SWT项目下就可运行。1.提示框MessageBox mb = new MessageBox(shell,SWT
- Android Service是分为两种:本地服务(Local Service): 同一个apk内被调用远程服务(Remote Servic
- 写在最前端1.SpringAOP中共有六种通知类型,只要我们自定义一个类实现对应的接口,它们全都是org.springframework.a
- 一、pom.xml<dependency> <groupId>net.sf.ehcache
- 本文实例讲述了Android开发实现popupWindow弹出窗口自定义布局与位置控制方法。分享给大家供大家参考,具体如下:布局文件:主布局
- 阿里、华为、腾讯Java技术面试题精选,具体内容如下JVM的类加载机制是什么?有哪些实现方式?类加载机制:类的加载指的是将类的.class文
- 注解从java5开始加入这一特性,发展到现在已然是遍地开花,在很多框架中得到了广泛的使用,用来简化程序中的配置。那充满争议的类型注解究竟是什
- 定时/计划功能主要使用的就是Timer对象,它在内部还是使用多线程的方式进行处理,所以它和线程技术还是有非常大的关联。Timer类主要作用就
- Android 处理OnItemClickListener时关于焦点颜色的设置问题  
- 开发语言:C#3.0 IDE:Visual Studio 2008 一、C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时