android toobar固定顶部,顶部toolbar被软键盘顶出

在Android开发中,遇到Toolbar被软键盘遮挡的问题。解决方案包括在布局中添加ScrollView并取消windowSoftInputMode的'adjustPan'设置。自定义SoftInputRelativeLayout类,通过重写computeSystemWindowInsets和onApplyWindowInsets方法来处理键盘弹出时的布局问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

碰巧我在开发的过程中也遇到了这个问题

我的解决方案如下

1.在所需要的布局上添加入Scrollview

...

2.如果有设置windowSoftInputMode="adjustPan" 取消该设置

自定义布局

package com.meizu.mzbbs.widget;

import android.content.Context;

import android.graphics.Rect;

import android.os.Build;

import android.util.AttributeSet;

import android.view.WindowInsets;

import android.widget.RelativeLayout;

/**

* Created by yangjingxuan on 16/6/15.

*/

public class SoftInputRelativeLayout extends RelativeLayout {

private int[] mInsets = new int[4];

public SoftInputRelativeLayout(Context context) {

super(context);

}

public SoftInputRelativeLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

public WindowInsets computeSystemWindowInsets(WindowInsets in, Rect outLocalInsets) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

// Intentionally do not modify the bottom inset. For some reason,

// if the bottom inset is modified, window resizing stops working.

// TODO: Figure out why.

mInsets[0] = outLocalInsets.left;

mInsets[1] = outLocalInsets.top;

mInsets[2] = outLocalInsets.right;

outLocalInsets.left = 0;

outLocalInsets.top = 0;

outLocalInsets.right = 0;

}

return super.computeSystemWindowInsets(in, outLocalInsets);

}

@Override

public 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;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值