使编辑动态增长和缩小

时间:2018-07-13 11:11:33

标签: android android-layout android-edittext

假设我有一个简单的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>

这看起来像这样:

enter image description here

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

关于该行为,我想更改两件事:

  • 这样做时停止扩展会导致按钮被推离屏幕
  • 不要逐渐扩展,立即跳到最大可能的大小

即我想要一个EditText这么大的

enter image description here

但是,只要您开始键入文字,它就会缩小到显示该按钮所需的程度。

enter image description here

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

2 个答案:

答案 0 :(得分:0)

  

尝试一下,希望对您有所帮助。

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

答案 1 :(得分:0)

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

  

Layout.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" />

...

  

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>
相关问题