BaseDialog.java
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.chudiangameplay.android.dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import com.chudiangameplay.android.util.StatusBarUtil_Dialog;
/**
* Created by 0 on 2019/7/17.
*/
public class BaseDialog extends Dialog {
public BaseDialog(@NonNull Context context) {
super(context);
}
public BaseDialog(@NonNull Context context, @StyleRes int themeResId) {
super(context, themeResId);
}
public BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StatusBarUtil_Dialog.setImmersiveStatusBar(this, true);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
/**
* addLayoutListener方法如下
*
* @param main 根布局
* @param scroll 需要显示的最下方View
*/
public void addLayoutListener(final View main, final View scroll) {
main.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//1、获取main在窗体的可视区域
main.getWindowVisibleDisplayFrame(rect);
//2、获取main在窗体的不可视区域高度,在键盘没有弹起时,main.getRootView().getHeight()调节度应该和rect.bottom高度一样
int mainInvisibleHeight = main.getRootView().getHeight() - rect.bottom;
int screenHeight = main.getRootView().getHeight();//屏幕高度
//3、不可见区域大于屏幕本身高度的1/4:说明键盘弹起了
if (mainInvisibleHeight > screenHeight / 4) {
int[] location = new int[2];
scroll.getLocationInWindow(location);
// 4、获取Scroll的窗体坐标,算出main需要滚动的高度
int srollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
//5、让界面整体上移键盘的高度
main.scrollBy(0, srollHeight);
} else {
//3、不可见区域小于屏幕高度1/4时,说明键盘隐藏了,把界面下移,移回到原有高度
main.scrollTo(0, 0);
}
}
});
}
}