700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android软键盘挡住输入框 设置adjustResize还无效 解决方案

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

时间:2022-12-26 03:56:50

相关推荐

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

* adjustPan是把整个界面向上平移,使输入框露出,不会改变界面的布局;

* adjustResize则是重新计算弹出软键盘之后的界面大小,相当于是用更少的界面区域去显示内容,输入框一般自然也就在内了,键盘被遮挡

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

(1)

adjustPan设置完可以,界面整体往上,设置adjustResize无效为啥?

原来我的Activity extends继承BaseActivty,改为Activity extendsAppCompatActivity就OK了。

(2)

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

/*** Created on /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);}@Overrideprotected boolean fitSystemWindows(Rect insets) {insets.top = 0;return super.fitSystemWindows(insets);}}

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

/*** xml层进行调用*/<com.test.widget.MyRelativeLayoutandroid: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);* 进行销毁*/@Overrideprotected void onDestroy() {super.onDestroy();ly_info.setFitsSystemWindows(false);}

/*** 记得在AndroidManifest.xml android:windowSoftInputMode="stateVisible|adjustResize"参数*/<activityandroid:name=".test.TestActivity" android:screenOrientation="portrait"android:theme="@style/MyAppTheme"android:windowSoftInputMode="stateVisible|adjustResize" />

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。