300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > android实现软键盘弹出 editText随键盘上移 背景不动

android实现软键盘弹出 editText随键盘上移 背景不动

时间:2019-03-01 14:11:19

相关推荐

android实现软键盘弹出 editText随键盘上移 背景不动

android实现软键盘弹出,editText随键盘上移,背景不动

前段时间有个妹子问我如题的需求,我就想,这种东西网上不是很多吗,自己试过才发现,基本都不行,各种设置配置文件的windowSoftInputMode,要么背景被压缩,要么背景向上移动,要么背景不动,但是editText没有跟着动,只能自己潜心研究,找到一种方案,虽然不完美,但是基本能满足大部分人的这类需求。

老规矩,先看效果,再贴代码:

键盘隐藏

键盘弹出

下面是代码部分:

public class EditMoveActivity extends Activity {private ImageView imageView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_edit_move);imageView = (ImageView) findViewById(R.id.imageView);Rect outRect = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();params.height = outRect.bottom - outRect.top;}}<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:overScrollMode="never"android:scrollbars="none"><LinearLayoutandroid:id="@+id/linearLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageViewandroid:id="@+id/imageView"android:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="centerCrop"android:src="@drawable/bg"/></LinearLayout></ScrollView><RelativeLayoutandroid:id="@+id/viewEdit"android:layout_width="match_parent"android:layout_height="60dp"android:layout_alignParentBottom="true"android:background="#88f1abcd"><EditTextandroid:id="@+id/editText"android:layout_width="match_parent"android:layout_height="60dp"android:layout_alignParentBottom="true" /></RelativeLayout></RelativeLayout><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="com.zhangxq.editmove"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".EditMoveActivity"android:windowSoftInputMode="adjustResize"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

可以看到代码很简单,需要注意的只有这么几点:

看第18行,效果基本上是依靠布局文件来实现的,布局文件最外层需要是RelativeLayout,(FrameLayout应该也行,我没有试过)。看第22行,不能移动的部分(这里使用ImageView举例)需要放在一个LinearLayout或者RelativeLayout里,并且外层需要套一个ScrollView,少一层都不行。看第10行,需要在代码中动态设置不能移动部分的高度(这里用ImageView举例)。看第69行,配置文件需要设置:android:windowSoftInputMode=“adjustResize”至于我说的不完美的地方是:如果你需要通过setFlag的方式隐藏状态栏,那么背景还是会上移。

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