【问题标题】:make edit grow and shrink dynamically使编辑动态增长和收缩
【发布时间】:2018-07-13 11:11:33
【问题描述】:

假设我有一个简单的EditText 和一个按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".DebugSimpleActivity">

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="?attr/actionBarSize"
        android:ems="10"
        android:gravity="start"
        android:hint="Type here"
        android:inputType="textMultiLine|textNoSuggestions"
        android:singleLine="false" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnParse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="parse"
            app:layout_constraintTop_toBottomOf="@+id/input" />
    </LinearLayout>
</LinearLayout>

这看起来像这样:

输入EditText 将扩展该可编辑文本字段,直到它最终不再适合屏幕,此时它将开始滚动。

关于这种行为,我想改变两点:

  • 停止展开会导致按钮被推到屏幕外
  • 不要逐渐扩大,立即跳到可能的最大尺寸

即我想要一个开始这么大的EditText

但是一旦您开始输入,就会尽可能地缩小以显示按钮。

有没有办法实现这种行为?

【问题讨论】:

  • 尝试将EditTextlayout_height更改为dp中的指定值,而不是wrap_content。
  • @Azhy 这会让我不得不根据运行应用程序的设备猜测正确的 dp 值。
  • 这不是什么大问题,你可以使用300dp 这样的东西使用屏幕尺寸来适应它。

标签: android android-layout android-edittext


【解决方案1】:

试试这个,希望对你有帮助。

<EditText
        android:id="@+id/xValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="xValue" />

【讨论】:

  • 呃...这是EditText,好吧。除此之外,它似乎与问题无关?
【解决方案2】:

我找到了答案,虽然您可能不需要这些更改,但我认为这是最好的答案,首先您应该将 Button 放在 EditText 上方并创建一个可绘制资源名称 Border.xml 和其他一些创建漂亮布局的更改:-

布局.xml

...

<Button
        android:id="@+id/btnParse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="parse"
        android:textSize="25sp" />

...

<EditText
    android:id="@+id/input"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:ems="10"
    android:gravity="start"
    android:hint="Type here"
    android:inputType="textMultiLine|textNoSuggestions"
    android:singleLine="false"        
    android:background="@drawable/Border" />

...

边框.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" ><solid android:color="@android:color/white" /><stroke android:width="1dp" android:color="#4fa5d5"/></shape>

【讨论】:

  • 所有这一切都是使背景变白。我也不想将按钮放在文本上方,而是将它们放在底部,靠近键盘以便于访问。因此,如果我设置 `android:layout_height="wrap_content"`` 按钮会再次被推离屏幕。
猜你喜欢
  • 1970-01-01
  • 2013-07-16
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多