|
1 |
| -# PopupLayout |
| 1 | +# PopupLayout |
2 | 2 |
|
3 | 3 |
|
4 | 4 | PopupLayout是通用弹出布局辅助库,允许开发者从顶部、底部、左侧、右侧和中心这五个位置弹出自己指定的布局。
|
@@ -26,10 +26,99 @@ dependencies {
|
26 | 26 |
|
27 | 27 | ### 基本使用
|
28 | 28 |
|
29 |
| -### 监听器 |
| 29 | +PopLayout的使用非常简单,只需要执行以下两步: |
| 30 | + |
| 31 | +**1.初始化PopLayout** |
| 32 | + |
| 33 | +通过调用静态方法init初始化PopLayout。这个方法有两个参数:第一个参数是Context对象,第二个参数代表弹出布局的内容,可以将layout资源Id或者View对象作为参数传入,如下所示: |
| 34 | + |
| 35 | +``` |
| 36 | +//1.使用layout资源Id作为弹出布局的内容 |
| 37 | +PopupLayout popupLayout=PopupLayout.init(MainActivity.this, R.layout.layout_left);//这里 |
| 38 | +
|
| 39 | +//2.使用View作为弹出布局的内容 |
| 40 | +View view=View.inflate(MainActivity.this,R.layout.layout_bottom_menu,null); |
| 41 | +PopupLayout popupLayout=PopupLayout.init(MainActivity.this,view); |
| 42 | +
|
| 43 | +``` |
| 44 | + |
| 45 | +**2.在指定位置显示弹出布局** |
| 46 | + |
| 47 | +PopupLayout可以从屏幕顶部、底部、左侧、右侧和中心弹出(默认从底部弹出),如下所示: |
| 48 | + |
| 49 | +``` |
| 50 | +//从左侧弹出 |
| 51 | +popupLayout.show(PopupLayout.POSITION_LEFT); |
| 52 | +//默认从底部弹出 |
| 53 | +popupLayout.show(); |
| 54 | +``` |
| 55 | + |
| 56 | +position的可选值: |
| 57 | +- POSITION_LEFT |
| 58 | +- POSITION_RIGHT |
| 59 | +- POSITION_CENTER |
| 60 | +- POSITION_TOP |
| 61 | +- POSITION_BOTTOM |
| 62 | + |
| 63 | +### 关闭弹出布局 |
| 64 | + |
| 65 | +点击弹出布局之外的区域,PopupLayout会被自动关闭。当然,也可以通过调用dismiss或hide方法手动关闭。这两个方法的区别在于hide只是隐藏弹出布局,并不会释放资源;而dismiss会销毁弹出布局并释放资源。通常情况下,建议使用dismiss方法。 |
| 66 | + |
| 67 | +**注意,**在Activity退出时必须使用dismiss方法销毁弹出布局,释放资源。 |
| 68 | + |
| 69 | +### 限制弹出布局的大小 |
| 70 | + |
| 71 | +默认情况下,PopupLayout会根据不同的弹出位置自动适配弹出布局的大小。具体而言,如果从顶部/底部弹出,宽度为MATCH_PARENT,高度为WRAP_CONTENT;如果从左侧/右侧弹出,宽度为WRAP_CONTENT,高度为MATCH_PARENT;如果从中心弹出,宽度为WRAP_CONTENT,高度为WRAP_CONTENT。 |
| 72 | + |
| 73 | +当然,PopupLayout也提供了setHeight和setWidth两个方法用于手动设置弹出布局的大小。这两个方法原型如下: |
| 74 | + |
| 75 | +``` |
| 76 | +/** |
| 77 | + * 设置弹出布局的高度 |
| 78 | + * @param height 高度 |
| 79 | + * @param dpMode 是否以dp为单位 |
| 80 | + */ |
| 81 | +public void setHeight(int height,boolean dpMode) |
| 82 | +
|
| 83 | +/** |
| 84 | + * 设置弹出布局的宽度 |
| 85 | + * @param width 宽度 |
| 86 | + * @param dpMode 是否以dp为单位 |
| 87 | + */ |
| 88 | +public void setWidth(int width,boolean dpMode) |
| 89 | +``` |
| 90 | + |
| 91 | +**注意,**这两个方法必须要在调用show方法之前使用才有效。 |
30 | 92 |
|
31 | 93 | ### 相关方法
|
32 | 94 |
|
| 95 | +| 方法名 | 返回值| 说明 | |
| 96 | +| :---------|:-----|:-----| |
| 97 | +| static init(Context context,@LayoutRes int contentLayoutId) | PopupLayout | 初始化弹出布局 | |
| 98 | +| static init(Context context,View contentView) | PopupLayout | 初始化弹出布局 | |
| 99 | +| show(int position) | void | 从指定位置显示弹出布局 | |
| 100 | +| show()| void | 默认从底部显示弹出布局 | |
| 101 | +| dismiss() | void | 隐藏弹出布局(同时会销毁弹出布局,释放资源),建议使用 | |
| 102 | +| hide() | void | 隐藏弹出布局(不会销毁弹出布局),不会触发DismissListener | |
| 103 | +| setUseRadius(boolean useRadius) | void | 是否使用圆角特性(默认使用) | |
| 104 | +| setHeight(int height,boolean dpMode) | void | 设置弹出布局的高度 | |
| 105 | +| setWidth(int width,boolean dpMode) | void | 设置弹出布局的宽度 | |
| 106 | +| setDismissListener(DismissListener dismissListener) | void | 设置Dismiss监听器(在弹出布局消失时触发) | |
| 107 | + |
| 108 | + |
| 109 | +### 监听器 |
| 110 | + |
| 111 | +PopupLayout允许使用者监听弹出布局的消失事件,只需要为PopupLayout设置DismissListener即可,如下所示: |
| 112 | + |
| 113 | +``` |
| 114 | +PopupLayout popupLayout=PopupLayout.init(MainActivity.this, R.layout.layout_bottom); |
| 115 | +popupLayout.setDismissListener(new PopupLayout.DismissListener() { |
| 116 | + @Override |
| 117 | + public void onDismiss() { |
| 118 | + Toast.makeText(MainActivity.this,"弹出窗口关闭",Toast.LENGTH_SHORT).show(); |
| 119 | + } |
| 120 | +});//添加监听器 |
| 121 | +``` |
33 | 122 |
|
34 | 123 | ## Blog
|
35 | 124 |
|
|
0 commit comments