在Android中动态添加Panel框架的实现代码
时间:2022-01-17 23:17:07
这里说是框架,说的大了点,其实没有那么复杂,只是一个容易扩展的基类而已。不过至少算是框架类的代码。
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);
}
标签:android,panel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Hutool工具实现验证码生成及Excel文件的导入和导出
2023-02-04 22:49:32
![](https://img.aspxhome.com/file/2023/7/76507_0s.png)
学习使用Android Chronometer计时器
2022-02-20 16:58:25
![](https://img.aspxhome.com/file/2023/0/110770_0s.jpg)
Java中一些关键字的使用技巧总结
2023-11-19 02:48:49
使用java实现http多线程断点下载文件(二)
2021-06-08 04:26:17
Java 数据结构与算法系列精讲之贪心算法
2023-09-12 17:56:13
![](https://img.aspxhome.com/file/2023/3/65303_0s.jpg)
IDEA 中使用 Hudi的示例代码
2021-08-27 21:51:04
![](https://img.aspxhome.com/file/2023/6/76236_0s.jpg)
C#基础:基于const与readonly的深入研究
2023-09-26 14:22:30
![](https://img.aspxhome.com/file/2023/7/102297_0s.jpg)
详解从零开始---用C#制作扫雷游戏
2022-06-14 13:33:12
![](https://img.aspxhome.com/file/2023/9/70399_0s.png)
浅谈maven单元测试设置代理
2022-03-14 20:23:04
C#采用HttpWebRequest实现保持会话上传文件到HTTP的方法
2023-07-18 17:20:29
Android系统制作自定义签名的例子
2023-02-02 05:10:04
![](https://img.aspxhome.com/file/2023/5/94985_0s.jpg)
C#将Unicode编码转换为汉字字符串的简单方法
2021-06-05 02:11:38
Java实现线程安全单例模式的五种方式的示例代码
2023-09-26 16:41:23
搭建一个基础的Resty项目框架
2021-06-08 11:12:06
浅谈Java自定义类加载器及JVM自带的类加载器之间的交互关系
2021-09-12 23:37:24
![](https://img.aspxhome.com/file/2023/0/66510_0s.jpg)
android实现简单的活动转盘
2022-11-29 08:40:35
![](https://img.aspxhome.com/file/2023/5/120065_0s.jpg)
apache ant进行zip解压缩操作示例分享
2021-11-08 09:16:03
SpringBoot整合MongoDB完整实例代码
2023-10-22 03:42:48
![](https://img.aspxhome.com/file/2023/5/81335_0s.jpg)
完美解决客户端webview持有的页面缓存,不会立即释放的问题
2021-09-23 03:30:50
Java实现简单台球游戏
2022-06-28 23:55:59
![](https://img.aspxhome.com/file/2023/1/62371_0s.png)