因此,过去几年来,这个问题在这里已经被问过几次了,没有一个解决方案对我有用。
问题是,当我使用AdjustPan
移动软键盘时,它隐藏了EditText
的一部分,如下所示:
我尝试的解决方案是:
大多数答案都包含使用AdjustPan/AdjustResize
,所以我只想说它们都没有提供所需的解决方案(打开键盘时显示整个EditText
)。
我当时在想也许在2019年可以解决此问题。
此布局的xml是:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
tools:context=".ChatActivity">
<androidx.appcompat.widget.ActionMenuView
android:id="@+id/actionbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@color/colorGrayBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.appcompat.widget.ActionMenuView>
<TextView
android:id="@+id/tv_This"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:fontFamily="@font/open_sans"
android:text="@string/ActivityChat_This"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/actionbar" />
<View
android:id="@+id/v_Line"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginLeft="24dp"
android:layout_marginTop="16dp"
android:layout_marginRight="24dp"
android:background="@color/colorLightGray"
app:layout_constraintTop_toBottomOf="@+id/tv_This" />
<androidx.cardview.widget.CardView
android:id="@+id/imagecard"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="@+id/actionbar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="@+id/ivPersonPic"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@null" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="@font/assistant_bold"
android:textColor="@color/colorLightPurple"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/actionbar"
app:layout_constraintStart_toEndOf="@+id/imagecard"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_Messages"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/constraint2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/v_Line"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraint2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintHeight_percent="0.1">
<EditText
android:id="@+id/et_Message"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="12dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/et_rounded"
android:fontFamily="@font/assistant"
android:gravity="center_vertical"
android:hint="@string/ActivityChat_Type"
android:inputType="textMultiLine|textPersonName"
android:paddingEnd="32dp"
android:paddingStart="32dp"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.85" />
<ImageButton
android:id="@+id/ib_Send"
style="?android:borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"
android:src="@drawable/ic_chats_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.1" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="0dp"
app:itemBackground="@color/colorWhite"
app:itemIconTint="@drawable/btm_nav"
app:itemTextColor="@drawable/btm_nav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.08"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu_btm_nav" />
</androidx.constraintlayout.widget.ConstraintLayout>
将您的基本布局包装在ScrollView中,将hight设置为match_parent并将fillViewport设置为true:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
和清单中:
<activity
android:name=".YourActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />
将其用作第二种解决方案,在这里我检查键盘出现后布局是否没有完全显示,在这种情况下,将视图滚动200像素。该代码也有一些注释。
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ScrollView;
public class MainActivity extends AppCompatActivity {
private ScrollView sView;
private int heightDiff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sView = findViewById(R.id.scrollView);
//Here we get the height of soft keyboard by observing changes of the scrollView's height.
sView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
heightDiff = sView.getRootView().getHeight() - sView.getHeight();
}
});
final EditText editText = findViewById(R.id.et_Message);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!isVisibleWhileSoftKeyboardShowing(editText) && hasFocus) {
sView.postDelayed(new Runnable() {
@Override
public void run() {
sView.smoothScrollBy(0, 200);
}
}, 500);
}
}
});
}
/**
* check if a view is currently visible in the screen or not
*
* @param view
* @return
*/
public boolean isVisibleWhileSoftKeyboardShowing(final View view) {
if (view == null) {
return false;
}
if (!view.isShown()) {
return false;
}
final Rect actualPosition = new Rect();
view.getGlobalVisibleRect(actualPosition);
final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight() - heightDiff);
return actualPosition.intersect(screen);
}
/**
* to get screen width
*
* @return
*/
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
/**
* to get screen height
*
* @return
*/
public static int getScreenHeight() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
}