//1、此layout作为最外层的layout;
//2、设置需要调整的view: setAdjustView(View view);
//3、如果需要控制输入框的显示与隐藏,可以实现OnInputViewVisibleListener接口;
public class SoftInputAdjustLayout extends RelativeLayout {
private static final String TAG = SoftInputAdjustLayout.class.getSimpleName();
private View adjustView;
public SoftInputAdjustLayout(Context context) {
this(context, null);
}
public SoftInputAdjustLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SoftInputAdjustLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
try {
Rect r = new Rect();
getWindowVisibleDisplayFrame(r);
int heightDiff = getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
if (onInputViewVisibleListener != null) {
onInputViewVisibleListener.onShowInputView();
}
} else {
if (onInputViewVisibleListener != null) {
onInputViewVisibleListener.onHideInputView();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* 需要调整的view
*
* @param adjustView
*/
public void setAdjustView(View adjustView) {
this.adjustView = adjustView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (adjustView != null) {
Rect rect = new Rect();
getWindowVisibleDisplayFrame(rect);
int totalHeight = getHeight();
int nowHeight = rect.bottom - rect.top;
int keyBoardHeight = totalHeight - nowHeight;
if (keyBoardHeight > totalHeight / 4) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(totalHeight, MeasureSpec.EXACTLY));
LayoutParams params = (LayoutParams) adjustView.getLayoutParams();
params.bottomMargin = keyBoardHeight;
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
LayoutParams params = (LayoutParams) adjustView.getLayoutParams();
params.bottomMargin = 0;
}
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
private OnInputViewVisibleListener onInputViewVisibleListener;
public void setOnInputViewVisibleListener(OnInputViewVisibleListener listener) {
onInputViewVisibleListener = listener;
}
public interface OnInputViewVisibleListener {
void onShowInputView();
void onHideInputView();
}
}
<!--此view用于获取焦点,当软件盘弹出时,根据此view来弹出,而此view在最顶部,且大小为0,所以软件盘弹出,不会把背景顶上去-->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有