簡體   English   中英

在約束布局中使用 guideLine 顯示軟鍵盤時如何設置項目?

[英]how to set items when soft keyboard is shown with using guideLine in constraint layout?

我在我的應用程序中使用ConstraintLayout

當顯示軟鍵盤時,我只想將按鈕移到上面,而其他項目不會向上移動。 我使用scrollView做到了這一點。

現在我使用guideLine來支持所有屏幕尺寸,但在顯示軟鍵盤后,所有項目再次向上移動。

我應該如何使用guideLine呢?

顯現:

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

Xml:

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

    <ScrollView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/button_register_sendVerification"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:fillViewport="true"
        >

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

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_register_topParent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.04" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_register_bottomParent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.96" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_register_leftParent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.08" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_register_rightParent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.92" />

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_register_topMobileNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.4"/>

            <androidx.constraintlayout.widget.Guideline
                android:id="@+id/guideline_register_bottomMobileNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.49"/>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout_register_mobileNumber"
                style="@style/TextInputLayout.All"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintLeft_toRightOf="@id/guideline_register_leftParent"
                app:layout_constraintRight_toLeftOf="@id/guideline_register_rightParent"
                app:layout_constraintTop_toTopOf="@+id/guideline_register_topMobileNumber"
                app:layout_constraintBottom_toBottomOf="@id/guideline_register_bottomMobileNumber">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textInputEditText_register_mobileNumber"
                    style="@style/TextInputEditText.All"
                    android:hint="@string/text_all_enterPhoneNumber"
                    android:inputType="phone"
                    android:maxLength="11"
                    android:textAlignment="center" />

            </com.google.android.material.textfield.TextInputLayout>


        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

    <Button
        android:id="@+id/button_register_sendVerification"
        style="@style/Button.all"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:text="@string/text_button_registerActivity_sendVerification"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

在此處輸入圖像描述

不幸的是,所有windowSoftInputModes都會移動文本視圖或隱藏按鈕。

如果你想使用 adjustResize 的adjustResize ,你將不得不容納一個ConstraintLayout ,它會在顯示和隱藏軟鍵盤時改變它的高度。 由於百分比Guideline將其 position 指定為ConstraintLayout高度(而非屏幕高度)的百分比,因此Guideline 的position 將隨着布局高度的變化而移動。

在您的情況下,解決此問題的最簡單方法是使用距ConstraintLayout頂部的偏移量指定Guideline位置。 由於此偏移量將與ConstraintLayout的頂部保持恆定距離,即使ConstraintLayout 的高度發生變化,它也會按照您的意願運行。

您還可以保留百分比Guidelines並以編程方式將它們轉換為固定的Guidelines 這樣,無論設備如何,您都可以獲得 40% 的偏移量。

以下代碼將用固定偏移量的Guideline 替換百分比 Guideline

private fun replaceGuideline(layout: ConstraintLayout, oldGuideline: Guideline) {
    require(layout.height > 0) { "ConstraintLayout must have gone through a layout pass." }
    Guideline(layout.context).apply {
        val lp = ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.WRAP_CONTENT,
            ConstraintLayout.LayoutParams.WRAP_CONTENT
        )
        lp.orientation = ConstraintLayout.LayoutParams.HORIZONTAL
        layoutParams = lp
        id = oldGuideline.id
        setGuidelineBegin(oldGuideline.top)
        layout.removeView(oldGuideline)
        layout.addView(this)
    }
}

您還可以指定 adjustNothing 的adjustNothing (文檔中奇怪地缺少它)。 然后你可以檢測軟鍵盤何時打開和關閉以調整按鈕的 position,盡管檢測軟鍵盤狀態非常棘手。

暫無
暫無

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

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