【问题标题】:Button not visible when keyboard is shown显示键盘时按钮不可见
【发布时间】:2021-11-14 08:42:03
【问题描述】:

我正在尝试实现一个功能,当显示键盘时 EditText 下面的按钮向上移动,这是一个登录片段,我希望这个功能类似于 Instagram 的功能。现在,当按下键盘时,EditText 向上移动但按钮被隐藏。我附上下面的xml。

<FrameLayout 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">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="150dp">

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:orientation="vertical">

                <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="20dp">

                    <ImageButton
                            android:layout_width="40dp"
                            android:layout_height="40dp"
                            android:layout_gravity="center"
                            android:scaleType="center"
                            android:src="@drawable/ic_back" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="20dp"
                    android:orientation="vertical" />

            <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/root_constraints"
                    android:layout_width="130dp"
                    android:layout_height="130dp"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="20dp"
                    android:elevation="20dp"
                   >

                <ImageView
                        android:id="@+id/std_imageView"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintLeft_toLeftOf="parent"
                        app:layout_constraintRight_toRightOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </FrameLayout>

        <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="30dp"
                android:layout_marginTop="144dp"
                android:layout_marginEnd="30dp"
                android:fontFamily="@font/montserrat_bold"
                android:textColor="#0B3148"
                android:textSize="48sp"
                android:textStyle="bold"/>

        <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="30dp"
                android:layout_marginEnd="30dp"
                android:fontFamily="@font/sf_bold"
                android:textColor="#0B3148"
                android:textSize="14sp"
                android:textStyle="bold"
                />

        <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="30dp"
                android:layout_marginEnd="30dp"
                android:fontFamily="@font/sf_regular"
                android:textColor="#0B3148"
                android:textSize="14sp" />

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

            <androidx.appcompat.widget.AppCompatEditText
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="30dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginEnd="30dp"
                    android:fontFamily="@font/roboto_light"
                    android:hint=""
                    android:visibility="gone"
                    android:inputType="number"
                    android:paddingStart="16dp"
                    android:textSize="14sp"
                    android:paddingTop="11dp"
                    android:paddingEnd="0dp"
                    android:paddingBottom="10dp"
                    android:textColorHint="@color/color_text"
                    android:text=""
                    android:drawablePadding="8dp"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent" />

            <androidx.appcompat.widget.AppCompatEditText
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginStart="30dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginEnd="30dp"
                    android:backgroundTint="#E4E4E4"
                    android:fontFamily="@font/roboto_light"
                    android:inputType="number"
                    android:paddingStart="16dp"
                    android:textSize="14sp"
                    android:paddingTop="11dp"
                    android:paddingEnd="0dp"
                    android:paddingBottom="10dp"
                    android:textColorHint="@color/color_text"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent" />

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@+id/et_countrycode"
                    android:visibility="gone"
                    android:textSize="12sp"
                    android:layout_marginTop="6dp"
                    app:layout_constraintStart_toStartOf="@+id/et_countrycode"
                    app:layout_constraintEnd_toEndOf="@+id/et_countrycode" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <Button
                android:id="@+id/finish"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:layout_marginHorizontal="30dp"
                android:background="@drawable/bg_button_1"
                android:textColor="#111B21"
                android:textSize="14sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />
    </LinearLayout>
</FrameLayout>

注意:我删除了一些文本和背景,但请记住所有这些视图都是必不可少的

编辑:我用adjustresize尝试了以下方法,但按钮没有与edittext重叠

<FrameLayout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="120dp"
                        android:orientation="vertical">

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:padding="20dp">

                            <ImageButton
                                android:layout_width="40dp"
                                android:layout_height="40dp"
                                android:layout_gravity="center"
                                android:scaleType="center"
                                android:src="@drawable/back" />
                        </LinearLayout>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_gravity="bottom"
                        android:layout_marginBottom="20dp"
                        android:orientation="vertical" />

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:id="@+id/root_constraints"
                        android:layout_width="130dp"
                        android:layout_height="130dp"
                        android:layout_gravity="top|center"
                        android:layout_marginTop="20dp"
                        android:elevation="20dp">

                        <ImageView
                            android:id="@+id/std_imageView"
                            android:layout_width="100dp"
                            android:layout_height="100dp"
                            android:backgroundTint="#38A19C"                                
                         app:layout_constraintBottom_toBottomOf="parent"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
                    </androidx.constraintlayout.widget.ConstraintLayout>
                </FrameLayout>

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30dp"
                    android:layout_marginTop="144dp"
                    android:layout_marginEnd="30dp"
                    android:fontFamily="@font/montserrat_bold"
                    android:textColor="#0B3148"
                    android:textSize="48sp"
                    android:textStyle="bold"
                    />

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30dp"
                    android:layout_marginEnd="30dp"
                    android:fontFamily="@font/sf_bold"
                    android:text="@string/welcome_to_isaac"
                    android:textColor="#0B3148"
                    android:textSize="14sp"
                    android:textStyle="bold" />

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="30dp"
                    android:layout_marginEnd="30dp"
                    android:fontFamily="@font/sf_regular"
                    android:textColor="#0B3148"
                    android:textSize="14sp" />

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <androidx.appcompat.widget.AppCompatEditText
                        android:id="@+id/et_countrycode"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginStart="30dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginEnd="30dp"
                        android:backgroundTint="#E4E4E4"
                        android:drawablePadding="8dp"
                        android:fontFamily="@font/roboto_light"
                        android:hint=""
                        android:inputType="number"
                        android:paddingStart="16dp"
                        android:paddingTop="11dp"
                        android:paddingEnd="0dp"
                        android:paddingBottom="10dp"
                        android:text=""
                        android:textColorHint="@color/color_text"
                        android:textSize="14sp"
                        android:visibility="gone"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <androidx.appcompat.widget.AppCompatEditText
                        android:id="@+id/et_email"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginStart="30dp"
                        android:layout_marginTop="30dp"
                        android:layout_marginEnd="30dp"
                        android:backgroundTint="#E4E4E4"
                        android:fontFamily="@font/roboto_light"
                        android:hint="@string/phone_number"
                        android:inputType="number"
                        android:paddingStart="16dp"
                        android:paddingTop="11dp"
                        android:paddingEnd="0dp"
                        android:paddingBottom="10dp"
                        android:textColorHint="@color/color_text"
                        android:textSize="14sp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="6dp"
                        android:textSize="12sp"
                        android:visibility="gone"
                        app:layout_constraintEnd_toEndOf="@+id/et_countrycode"
                        app:layout_constraintStart_toStartOf="@+id/et_countrycode"
                        app:layout_constraintTop_toBottomOf="@+id/et_countrycode" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </LinearLayout>

        </ScrollView>

        <Button
            android:id="@+id/bt_finish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="30dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/bg_button_1"
            android:text="@string/continue_txt"
            android:textColor="#111B21"
            android:textSize="14sp"
            app:layout_constraintTop_toBottomOf="@id/scrollView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    使用 ConstraintLayout 将按钮定位到屏幕底部,然后添加。

    <activity
        android:name=".activity"
        android:windowSoftInputMode="adjustResize"
        android:exported="false" />
    

    ...在将承载该 Fragment 布局的 Activity 中。

    注意

    按钮的布局应该是这样的:

    <androidx.constraintlayout.widget.ConstraintLayout>
    
         <Button
            android:id="@+id/finish"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            ... /> 
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    ...但是现在在您的代码中,我可以在 LinearLayout 中看到按钮。更改布局以使按钮位于 ConstraintLayout 的底部,并使该 ConstraintLayout 成为该按钮的父级。 ConstraintLayout 也应该填满整个屏幕。

    编辑:

    当您使用windowSoftInputMode="adjustResize 时,系统会将元素定位在键盘顶部的屏幕底部,并使您的布局缩小。所以你需要附加一个 ScrollView 来让你的布局可以滚动。

    像这样:

     <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
             android:layout_height="match_parent"
             ...>
        
        // Position this scrollView to fill the entire screen
    
          <ScrollView
             android:layout_width="0dp"
             android:layout_height="0dp"
             ...>
    
        // This scroll view layout would contain all your other layout views
    
          </ScrollView>    
    
           <Button
              android:id="@+id/finish"
              android:layout_width="match_parent"
              android:layout_height="wrap_content" 
              ... /> 
        
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    工作原理

    按钮不应该是ScrollView的子视图,应该是根视图的子视图,即ConstraintLayout 填充屏幕,然后按钮定位到 根视图底部ScrollView 的底部应定位到Buttontop。这样一来,ScrollView子视图可滚动的 并且按钮停留在键盘的顶部

    【讨论】:

    • 但是那不会遵循提供的UI,按钮应该在输入字段下方12dp
    • 那么您应该将androd:marginTop="12dp" 添加到您的代码中
    • 按钮由系统自动定位并应用边距,但您仍需要将整个布局文件放在 ScrollView 中,以便布局可滚动。
    • 另外,不要将 match_parentConstraintLayout 一起使用,而应使用 match_constraint
    • 我尝试了你的方法,但由于某种原因,现在按钮与编辑文本重叠,问题是之前编辑文本和键盘之间的差距可以忽略不计,所以我想这可能是个问题,但什么可以做吗?当根视图更改为滚动视图时会出现此错误,但在我手动滚动到该位置之前仍然隐藏按钮
    【解决方案2】:

    转到您的 AndroidMenifest 文件,在您的内部 &lt;activity&gt; 标签放 android:windowSoftInputMode = "adjustpan" 它会在显示键盘后处理视图。如果adjustpan不起作用,还有更多属性,请根据您的需要使用一个方面。

    示例:

    <activity
        android:name=".YourActivityName"
        android:windowSoftInputMode="adjustPan"
        android:exported="false" />
    

    【讨论】:

    • 我使用了所有的属性,它们都不起作用
    猜你喜欢
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 2014-06-13
    • 1970-01-01
    相关资源
    最近更新 更多