簡體   English   中英

Android. 鍵盤打開時如何讓按鈕顯示在鍵盤上方?

[英]Android. How to show button to top of keyboard when it's opened?

我需要將我的按鈕顯示在軟鍵盤的頂部(當它打開時)。 為此,我實現了這個https://medium.com/@madalinnita/android-how-to-move-views-above-keyboard-when-its-opened-quick-secure-solution-90188c4d7b15

我的布局結構:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical">
 <androidx.core.widget.NestedScrollView
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="true"
       app:layout_constraintBottom_toTopOf="@+id/bottomContainer"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

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

           <EditText..../>
           <EditText..../>

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

 <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:gravity="end"/>

</androidx.constraintlayout.widget.ConstraintLayout>

一切正常,但在小型設備中,我的按鈕覆蓋了編輯文本。 有可能修復它嗎? 我沒有任何想法。 即使在小型設備上,我也需要以某種方式使按鈕和鍵盤出現在編輯文本下方。

請幫我。

您可以將整個視圖包裝在 ScrollView 中,這樣您就可以在鍵盤打開時滾動視圖。 在此示例中,我使用的是 LinearLayout,您也可以使用其他布局。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

並將這行代碼添加到特定活動的清單中:

android:windowSoftInputMode="adjustResize"

添加此行 -> android:windowSoftInputMode="adjustResize" ,在清單中的當前活動標簽中,如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        ...>
        
        <activity 
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize"
            ...
            />
        ...
    </application>
</manifest>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM