【问题标题】:Android: TextInputLayout doesn’t fit into page and prevents the page from scrolling inside NestedScrollViewAndroid:TextInputLayout 不适合页面并阻止页面在 NestedScrollView 内滚动
【发布时间】:2022-01-24 06:45:46
【问题描述】:

我的布局文件的结构:协调器布局 - NestedScrollView - 协调器布局 - 我希望能够滚动的对象

带有 TextInputEditText 的 TextInputLayout 位于屏幕底部,填充后会超出屏幕范围。当我折叠键盘时,它仍然无法滚动。

注意:我不想将 maxLines 设置为 Edittext 并使其自身可滚动。我希望它与其他元素一起滚动。

但是,例如,如果我将 TextView 放在屏幕底部并且它不适合,则页面是可滚动的。有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        >
        
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <!--other items i want to scroll-->
            
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/descriptionLayout"
                android:layout_width="348dp"
                android:layout_height="200dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="545dp"
                android:ems="16"
                android:fontFamily="@font/montserrat"
                android:hint="Add image description (optional)"
                android:inputType="textMultiLine|textAutoCorrect"
                app:counterEnabled="true"
                app:counterMaxLength="256"
                app:boxBackgroundColor="@color/white"
                app:srcCompat="@drawable/ic_round_cloud_upload_24"
                android:textColor="#68B2A0"
                android:textColorHint="#68B2A0"
                app:hintTextColor="#68B2A0"
                app:layout_anchor="@id/titleLayout"
                app:boxStrokeErrorColor="#F75010"
                app:boxStrokeColor="#68B2A0"
                app:boxStrokeWidth="2dp"
                app:errorIconTint="@color/error"
                app:errorTextColor="@color/error"
                app:layout_anchorGravity="center|bottom"
                app:counterOverflowTextColor="@color/error"
                   style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                app:counterTextColor="@color/main_green"
                >
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/descriptionTxt"
                    android:ems="16"
                    android:textSize="18sp"
                    android:textColor="#68B2A0"
                    android:fontFamily="@font/montserrat"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:maxLines="7"
                    />

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

        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    </androidx.core.widget.NestedScrollView>
    
    <!--bottom app bar-->
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-layout android-xml android-textinputlayout android-nestedscrollview


    【解决方案1】:

    这是因为你硬设置了android:layout_marginTop="545dp"。尽量不要那样做。我也认为在你的情况下使用 costaint layout 更好。只需将嵌套滚动视图中的 &lt;androidx.coordinatorlayout.widget.CoordinatorLayout&gt; 更改为 &lt;androidx.constraintlayout.widget.ConstraintLayout&gt; 并添加此属性:

    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent
    

    如果您需要全屏显示嵌套视图,只需将android:layout_height="wrap_content" 更改为android:layout_height="match_parent"

    【讨论】:

    • 将 coordinatorLayout 更改为 contraintLayout 已修复,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 2014-05-24
    相关资源
    最近更新 更多