版权声明:本文为博主原创文章,转载请声明出处 https://blog.csdn.net/qq_16666847/article/details/79667911
先看未修复之前的UI图
这是修复后的UI 动图
git图片能看到布局是随之变化的,怎么做呢?
- 第一步 添加配置
在Activity配置文件中中添加android:windowSoftInputMode="stateAlwaysVisible|adjustResize"
- 第二步 监听键盘弹起
viewMove 移动的View;
container 要做修复的布局;
viewStationary固定的View的最下部布局;
@Override
protected void onStart() {
super.onStart();
int height = ScreenUtils.getScreenHeight();//获取屏幕高度
viewMove.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) ->//监听浮动的View 布局变化
{
if (height - bottom > height * 0.3f) {//默认布局变化大于0.3就是输入法弹出了,这里判断输入法是否弹出
LogUtil.e("-----up ----");
float staY = viewStationary.getY();
float moveY = viewMove.getY();
float scroll = moveY - staY;
int fix = SizeUtils.dp2px(20);//这里设定了固定View 和 浮动View的距离是最小20dp,大于20dp后不用处理
if (scroll < fix) {
ViewAnimator.animate(container).translationY(scroll - fix).duration(200).start();//动画处理,自行处理
}
} else {
LogUtil.e("-----down ----");
ViewAnimator.animate(container).dp().translationY(0).duration(200).start();
}
});
}
此乃布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="49dp"
android:layout_height="55dp"
android:layout_marginStart="@dimen/dp_10"
android:padding="20dp"
android:src="@drawable/back_arrow"
android:tint="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="@string/forget_password"
android:textColor="@color/white"
android:textSize="@dimen/sp_14"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="70dp"
android:gravity="center_vertical"
android:text="登录所依"
android:textColor="@color/white"
android:textSize="28sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/tvTipPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="@string/phone_num"
android:textColor="@color/white"
android:textSize="@dimen/sp_14"
app:layout_constraintTop_toBottomOf="@id/tv1"/>
<TextView
android:id="@+id/tvPhoneLocal"
android:layout_width="45dp"
android:layout_height="35dp"
android:layout_marginStart="20dp"
android:background="@drawable/shape_white_radius_3"
android:text="+86"
android:textColor="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTipPhone"/>
<EditText
android:id="@+id/editPhone"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="10dp"
android:background="@null"
android:inputType="phone"
android:textColor="@color/white"
app:layout_constraintLeft_toRightOf="@id/tvPhoneLocal"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTipPhone"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="@dimen/dp_10"
android:background="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/editPhone"/>
<TextView
android:id="@+id/tvTipPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_40"
android:padding="20dp"
android:text="@string/password"
android:textColor="@color/white"
android:textSize="@dimen/sp_14"
app:layout_constraintTop_toBottomOf="@id/tvTipPhone"/>
<TextView
android:id="@+id/tvTipPasswordShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_40"
android:padding="20dp"
android:text="@string/password_show"
android:textColor="@color/white"
android:textSize="@dimen/sp_14"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvTipPhone"/>
<EditText
android:id="@+id/editPassword"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="10dp"
android:background="@null"
android:digits="@string/digits"
android:inputType="textPassword"
android:textColor="@color/white"
app:layout_constraintLeft_toLeftOf="@id/tvPhoneLocal"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvTipPassword"/>
<View
android:id="@+id/viewStationary"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:layout_marginTop="@dimen/dp_10"
android:background="@color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/editPassword"/>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
android:id="@+id/viewMove"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="20dp"
android:src="@drawable/right_arrow_cir"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_margin="20dp"
android:background="@drawable/shape_white_radius_45_bound"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/login_wx"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>
</FrameLayout>