出现键盘时调整ViewPager2的大小

时间:2020-09-02 15:01:23

标签: android kotlin

我正在尝试在应用程序上使用ViewPager2,但是在实现它时遇到了一些问题。

问题:

当我单击EditText时,出现键盘,并且 ViewPager2调整大小,更改Height

我想要的:

当我点击EditText时,出现键盘并移动 查看而不调整ViewPager2的大小。

如何防止ViewPage2调整大小并仅在出现键盘时向上移动视图?
有什么属性要设置吗?

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="stateVisible">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的ViewPager2

<?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"
    tools:context=".ui.main.BlankFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="403dp"
        android:layout_height="17dp"
        android:text="Something Something Something Something"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

<!--    <androidx.viewpager.widget.ViewPager-->
<!--        android:id="@+id/view_pager"-->
<!--        android:visibility="gone"-->
<!--        android:layout_width="match_parent"-->
<!--        android:layout_height="0dp"-->
<!--        app:layout_constraintBottom_toBottomOf="parent"-->
<!--        app:layout_constraintTop_toBottomOf="@id/textView"-->
<!--        app:layout_behavior="@string/appbar_scrolling_view_behavior" />-->

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager2"
        android:visibility="visible"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@id/textView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是在视图寻呼机内部调用的片段

<?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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.PlaceholderFragment">

    <TextView
        android:id="@+id/section_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginEnd="@dimen/activity_horizontal_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/constraintLayout"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintTop_creator="1"
        tools:text="adasdasdasdasda"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb asdasdasdasdoahdjasd csdgv dfs bvdf b dsfb dsf bd fb dfb "
        app:layout_constraintBottom_toTopOf="@+id/editText"
        app:layout_constraintTop_toBottomOf="@+id/section_label" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

布局错误/我想要什么

The problem layout The layout that I want

1 个答案:

答案 0 :(得分:0)

尝试将其添加到AndroidManifest的“活动”中:

checkbox.addEventListener('change', (e) => {
  if (e.target.checked) {
    // add a class or specific styles here
  } else {
    // remove prev updates
  }
});

您还可以测试android:windowSoftInputMode="adjustPan" adjustResize,以了解哪种方法最适合您。