出现键盘后调整活动大小
Posted
技术标签:
【中文标题】出现键盘后调整活动大小【英文标题】:Activity resized after keyboard is appears 【发布时间】:2020-01-29 11:22:12 【问题描述】:我有一个活动,其中包含一个由包含标记添加的片段(在活动的顶部)和一个主要的中心片段。主片段还包含 editText 小部件。当我单击 edittext 键盘时,它会根据所选的窗口显示出来,SoftInputMode 键盘会向上移动窗口(上部片段消失在屏幕后面)或与主片段上键盘下方的按钮重叠。 (两种情况下都不会出现滚动)。
我需要可滚动的片段窗口,但它不可能吗?怎么样?
Xmls:
活动:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="?attr/bg_second"
android:keepScreenOn="true">
<FrameLayout
android:id="@+id/fl_work_container"
android:layout_
android:layout_
android:layout_gravity="bottom|right|end"
tools:ignore="RtlHardcoded" />
<include layout="@layout/view_menu_main" />
主要片段:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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_
android:layout_>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_
android:layout_
android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/tv_tariff_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textInputLayout">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_border"
android:layout_
android:layout_
android:layout_marginBottom="@dimen/space_large_m"
android:background="?attr/bg_main"
android:minHeight="120dp"
android:paddingStart="@dimen/space_medium_xl"
android:paddingTop="@dimen/space_medium_l"
android:paddingEnd="@dimen/space_medium_xl" />
<TextView
android:id="@+id/tv_empty_point"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/space_medium_s"
android:text="@string/road_to_empty_text"
android:textAlignment="center"
android:textColor="?attr/text_main"
tools:textColor="@color/black" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_
android:layout_
android:layout_marginEnd="@dimen/space_medium_xl"
android:tint="@android:color/white"
app:backgroundTint="@color/bg_button_light"
app:layout_anchor="@id/rv_border"
app:layout_anchorGravity="bottom|right|end"
app:srcCompat="@android:drawable/ic_input_add" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_
android:layout_
android:layout_marginTop="70dp"
android:paddingStart="@dimen/space_small_l"
android:paddingEnd="@dimen/space_small_l"
android:theme="@style/TextInputLayout"
app:layout_constraintBottom_toTopOf="@id/tv_tariff_label"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="+7949919">
<EditText
android:id="@+id/phone_number"
android:layout_
android:layout_
android:hint="@string/phone_number_of_client"
android:importantForAutofill="no"
android:inputType="phone"
android:maxLength="15"
android:maxLines="1"
android:singleLine="true"
android:textColor="?attr/text_main"
android:textColorHighlight="@android:color/darker_gray"
android:textColorHint="@android:color/darker_gray"
tools:textColor="@color/black" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/tv_tariff_label"
android:layout_
android:layout_
android:layout_marginTop="40dp"
android:padding="@dimen/space_medium_s"
android:singleLine="true"
android:text="@string/work_main_tariff"
android:textColor="?attr/text_main"
android:textSize="@dimen/font_size_huge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textInputLayout"
tools:textColor="@color/black" />
<LinearLayout
android:id="@+id/ll_tariff_view"
android:layout_
android:layout_
android:background="?attr/bg_second"
android:orientation="horizontal"
android:weightSum="3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_tariff_label">
<TextView
android:id="@+id/tv_tariff"
android:layout_
android:layout_
android:layout_weight="1.8"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:paddingStart="@dimen/space_medium_s"
android:paddingEnd="@dimen/space_medium_s"
android:tag="tariff"
android:textColor="?attr/text_main"
android:textSize="@dimen/font_size_huge"
tools:text="My tariff"
tools:textColor="@color/black" />
<Button
android:id="@+id/add_opt_btn"
android:layout_
android:layout_
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="4dp"
android:layout_weight="1"
android:background="@drawable/custom_button_green"
android:text="@string/options_title"
android:textColor="?attr/text_button"
android:textSize="@dimen/font_size_large"
android:visibility="invisible"
tools:textColor="@color/white" />
<ProgressBar
android:id="@+id/p_bar_options"
android:layout_
android:layout_
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="4dp"
android:layout_weight="1.2"
android:padding="4dp"
android:text="@string/options_title"
android:textColor="?attr/text_button"
android:textSize="@dimen/font_size_large"
android:theme="@style/accentStyle"
android:visibility="gone"
tools:textColor="@color/white" />
</LinearLayout>
<Button
android:id="@+id/btn_start_work"
android:layout_
android:layout_
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:background="@drawable/custom_button_green"
android:text="@string/work_btn_begin"
android:textColor="?attr/text_button"
android:textSize="@dimen/font_size_large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ll_tariff_view"
tools:textColor="@color/white" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_
android:layout_
android:layout_marginTop="50dp"
android:theme="@style/accentStyle"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ll_tariff_view"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
【参考方案1】:您可以将以下代码用于您的活动清单文件,我认为它应该可以解决您的问题
android:windowSoftInputMode="adjustPan|adjustResize"
【讨论】:
以上是关于出现键盘后调整活动大小的主要内容,如果未能解决你的问题,请参考以下文章