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" />
 

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

猜你喜欢

转载自blog.csdn.net/zyy_give/article/details/103563466
今日推荐