首页 > 解决方案 > 允许嵌套滚动同时允许编辑文本稍微扩展(xml)

问题描述

我想要的是能够拥有一个EditText可以从 5 行扩展到 10 行文本的内容。当它达到 10 行时,它开始以嵌套方式滚动。即当达到编辑文本滚动的限制时,可以滚动外部页面视图。到目前为止我尝试过的是这样,但它不允许嵌套滚动:

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <EditText
            android:id="@+id/edit_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="10"
            android:background="@android:color/transparent"
            android:gravity="top"
            android:paddingStart="12dp"
            android:paddingEnd="12dp"
            android:textSize="15sp"
            tools:text="Some description \n\n\n\n\n asdf \n asdf \n \n adsf \n asdf \n asdf \n asdf \n asfd " />
</androidx.core.widget.NestedScrollView>

但是,如果我在 上设置特定的布局高度,它确实有效NesteScrollView,但那将不允许EditText扩展。

还。我不想为此使用代码。我知道它可能会覆盖触摸事件等,但我正在寻找一种更清洁的方法。

标签: androidandroid-layoutandroid-nestedscrollview

解决方案


尝试允许它EditText成为一个滚动容器,并确保它通知其父 NestedScrollView:

android:isScrollContainer="true"       
android:nestedScrollingEnabled="true"

推荐阅读