Android软键盘挡住输入框,设置adjustResize还无效,解决方案

* adjustPan是把整个界面向上平移,使输入框露出,不会改变界面的布局;
* adjustResize则是重新计算弹出软键盘之后的界面大小,相当于是用更少的界面区域去显示内容,输入框一般自然也就在内了,键盘被遮挡

需求:不让布局把title直接顶上去,不要键盘挡住输入框

(1)

adjustPan设置完可以,界面整体往上,设置adjustResize无效为啥?
原来我的Activity extends 继承BaseActivty,改为Activity extends AppCompatActivity就OK了。

(2)
 如果说BaseActivty有些对Activty做了一些抽象方法,或者Base层注册了EventBus事件,也要用到咋办?
 

/**
 * Created on 2019/12/16.
 * 如果最外层定义的LinearLayout也可以,extends LinearLayout,
   次布局是RelativeLayout。
 */
public class MyRelativeLayout extends RelativeLayout {

    public MyRelativeLayout(Context context) {
        super(context);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected boolean fitSystemWindows(Rect insets) {
        insets.top = 0;
        return super.fitSystemWindows(insets);
    }

}

 

将原先的xml布局的根ViewGroup换成我们自定义的ViewGroup,引用,在代码层设置

/**
 * xml层进行调用
 */
<com.test.widget.MyRelativeLayout
    android:layout_width="match_parent"
    android:id="@+id/ly_info"
    android:layout_height="match_parent"/>
/**
 * xml层进行调用
 */
MyRelativeLayout linearLayout = (MyRelativeLayout) findViewById(R.id.ly_info); 
linearLayout.setFitsSystemWindows(true);


 /**
  * 最好在Activity或Fragment销毁时调用linearLayout.setFitsSystemWindows(false);
  * 进行销毁
  */
 @Override
 protected void onDestroy() {
 super.onDestroy();
 ly_info.setFitsSystemWindows(false);
 }
/**
 * 记得在AndroidManifest.xml android:windowSoftInputMode="stateVisible|adjustResize"参数
 */
<activity
     android:name=".test.TestActivity"           
     android:screenOrientation="portrait"
     android:theme="@style/MyAppTheme"
     android:windowSoftInputMode="stateVisible|adjustResize" />
 

 

亲测成功,如有问题,不吝赐教。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值