【问题标题】:How to MultiLine EditText with the cursor with the padding starting on top?如何使用从顶部开始的填充光标使用 MultiLine EditText?
【发布时间】:2019-09-02 02:16:26
【问题描述】:

我正在使用EditText 发送消息。 EditText 的填充是 30sdp

问题是当我点击这个EditText时,光标在EditText的中间闪烁,而我希望光标应该在EditText的顶部闪烁。

这是我的代码。

<EditText
    android:id="@+id/edremarkspetrolbill"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvpetrolbillremarks"
    android:layout_marginLeft="@dimen/_7sdp"
    android:layout_marginTop="@dimen/_10sdp"
    android:layout_marginRight="@dimen/_10sdp"
    android:background="@drawable/background_boxsearch"
    android:gravity="top"
    android:inputType="textMultiLine"
    android:padding="@dimen/_30sdp"
    android:textColor="@color/colorbluewh"
    android:textCursorDrawable="@color/colorbluewh" />

【问题讨论】:

  • 请提供您的 XML sn-p。
  • 填充是内部间距。您是在告诉EditText 将其所有内容向内移动 30,在每一侧。这包括文本行和光标。如果您不想在顶部使用该填充,则删除 padding 属性,并单独设置其他三个;即 paddingLeft、paddingRightpaddingBottom

标签: android android-edittext cursor padding


【解决方案1】:

您应该像这样更改您的代码(我已将 100dp 作为高度,但我建议您将 EditText 放入 CostraintLayout 并使用 match_constraint)

<EditText
        android:id="@+id/edremarkspetrolbill"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/tvpetrolbillremarks"
        android:layout_marginLeft="@dimen/_7sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:paddingStart="30dp"
        android:paddingEnd="30dp"
        android:background="@drawable/background_boxsearch"
        android:gravity="top|left"
        android:inputType="textMultiLine"
        android:textColor="@color/colorbluewh"
        android:textCursorDrawable="@color/colorbluewh" />

【讨论】:

  • 谢谢你的建议,但很抱歉你说所有方法都适用但不能解决我的问题,不能使用约束布局。
  • 你试过我的xml了吗?
  • Yes.try to your xml only you modified layout_height.but sorry not work .
  • 并删除了填充,这就是我告诉你复制并粘贴它的原因:)
【解决方案2】:

像下面这样使用重力

android:gravity="top|left"
android:inputType="textMultiLine"

【讨论】:

  • 您只使用了重力 :"top" 。按照我的回答中提到的方式使用它->“top|left”
【解决方案3】:

android:gravity="top" 是正确的,但你有android:layout_height="wrap_content",这是个问题。您的编辑文本高度接近您的文本大小+填充,光标位于编辑文本的顶部,但您看不到它。如果您现在将高度设置为编辑文本,您将看到光标所在的位置。或者如果您想要高度wrap_content,您可以提供minHeight 参数,然后编辑文本将使用minHeight 膨胀,如果文本不适合它的高度,则会扩展。您还可以在gravity 字段中不仅提供一个参数,只需将参数附加到|。例如:top|starttop|end

【讨论】:

    【解决方案4】:

    在多行编辑文本中将重力设置为顶部

    android:gravity="top"
    android:inputType="textMultiLine"
    

    【讨论】:

      【解决方案5】:

      在您的编辑文本中添加此属性:android:gravity="top"

      【讨论】:

        猜你喜欢
        • 2012-05-17
        • 1970-01-01
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多