【问题标题】:EditText's height does not expand to its parent's heightEditText 的高度不会扩展到其父级的高度
【发布时间】:2019-03-26 12:42:48
【问题描述】:

我在高度 = match_parent 的滚动视图中放置了一个编辑文本,并希望它的高度等于滚动视图,但事实并非如此。它的高度类似于wrap_content,这意味着如果 EditText 中没有文本,我必须将光标指向第一“行”才能弹出软键盘,我想要的是我可以触摸屏幕上的任何位置(滚动视图)和软键盘弹出。

以下是布局,感谢您的帮助。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:gravity="top"
            android:padding="10dp"
            android:textSize="16sp"
            android:textColor="@android:color/black">
        </EditText>
    </ScrollView>

    <Button
        android:id="@+id/btnPaste"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/paste"
        android:gravity="center"
        android:onClick="pasteData"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="6dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/rounded_corner_button"
        android:textColor="@android:color/white"/>
</LinearLayout>

布局捕获:

【问题讨论】:

  • 你想让你的edittext可以滚动吗?
  • @primo 它是可滚动的,这就是我将它放在 ScrollView 中的原因。
  • 你不需要为此使用滚动视图设置 android:layout_height="0dp" android:layout_weight="1" 到 EditText 并检查它是否工作
  • @VamsiSmart 感谢您的回复,我知道 EditText 它自己可以滚动,但它会有点“滞后”,将它放在 ScrollView 中将使“滚动”更顺畅。跨度>
  • 尝试这里给出的解决方案stackoverflow.com/a/24428854/8101634

标签: android android-edittext height parent


【解决方案1】:

在您的 ScrollView 中添加这一行。

android:fillViewport="true"

【讨论】:

    【解决方案2】:

    你可以通过改变minLines属性来控制编辑文本

    例如:

    <EditText
                android:id="@+id/ed_comment"
                style="@style/EditText.NoBackground"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="top"
                android:hint="@string/write_comment_hint"
                android:minLines="40"
                android:textSize="16sp"/>
    

    而不是通过 scrollView 包装 EditText 将此属性添加到 editText

    android:scrollbars="vertical"
    

    结果:

    【讨论】:

    • 感谢您的评论,我知道我可以使用选项scrollBars 使 EditText 变得可滚动,但此选项不像滚动视图那样“流畅”。例如,如果滚动条很长,则使用 ScrollView 只需轻轻一按即可向下/向上滚动,并且屏幕随之运行。但是只有这个选项,你不能,你需要一直向上/向下滑动直到顶部/底部。
    【解决方案3】:

    在 xml 文件中将其实现到您的 EditText:

    android:scrollbars="vertical"
    

    在 onCreate() 中实现:

     editText.setMovementMethod(new ScrollingMovementMethod());
    

    然后,如果你只将它用于 textview,你就不需要滚动视图,它只是让你的 textview 可以滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2010-09-27
      相关资源
      最近更新 更多