本文目录:
1.开启沉浸式状态栏后,输入法弹出异常,adjustResize失效(api<21时出现)
2.ScrollView 嵌套ListView 后 高度仅有一行数据
1.开启沉浸式状态栏后,输入法弹出异常,adjustResize失效(api<21时出现)
解决办法 - 原文地址:stackflow
//在要弹出的根布局中添加
android:fitsSystemWindows="true"
//然后界面高度就不正常了(出现了空白高度,估计是预留的状态栏高度);
//咱们可以重写Layout的fitSystemWindows的方法来解决此问题
//然后api20又出问题了,还需要重写windowinsetsonapplywindowinsets方法 。。。
//具体复写的类如下
public class MyResizeLinearLayout extends LinearLayout{
private int[] mInsets = new int[4];
public MyResizeLinearLayout(Context context) {
super(context);
}
public MyResizeLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyResizeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected final boolean fitSystemWindows(Rect insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
mInsets[0] = insets.left;
mInsets[1] = insets.top;
mInsets[2] = insets.right;
insets.left = 0;
insets.top = 0;
insets.right = 0;
}
return super.fitSystemWindows(insets);
}
@Override
public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
mInsets[0] = insets.getSystemWindowInsetLeft();
mInsets[1] = insets.getSystemWindowInsetTop();
mInsets[2] = insets.getSystemWindowInsetRight();
return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
insets.getSystemWindowInsetBottom()));
} else {
return insets;
}
}
}
相关文章推荐:Android爬坑之旅:软键盘挡住输入框问题的终极解决方案(//www.greatytc.com/p/306482e17080)
2.ScrollView 嵌套ListView或RecycleView 后 高度仅有一行数据的高度
分析原因:ScrollView 无法预估ListView 的高度,所以高度为item的高度
解决方法 : 手动计算高度
private void refreshHight() {
int totalHeight = 0;
for (int i = 0, len = lvListPay.getCount(); i < len; i++) {
View listItem = adapter.getView(i, null, lvListPay);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lvListPay.getLayoutParams();
params.height = totalHeight + (lvListPay.getDividerHeight() * (adapter.getCount() - 1));
lvListPay.setLayoutParams(params);
}
当item高度固定可以使用下面的算法优化
private void refreshHight() {
int totalHeight = 0;
View listItem = adapter.getView(0, null, lvListPay);
listItem.measure(0, 0);
totalHeight = listItem.getMeasuredHeight() * lvListPay.getCount();
ViewGroup.LayoutParams params = lvListPay.getLayoutParams();
params.height = totalHeight + (lvListPay.getDividerHeight() * (adapter.getCount() - 1));
lvListPay.setLayoutParams(params);
}