android - 如何使 EditText 在 Constraintlayout 中正确换行
问题描述
大家好,我有一个问题,我已经痛了 2 天了。这似乎是一个简单的问题,但我似乎无法解决它。基本上我正在制作一个莫尔斯电码翻译应用程序当用户在 EditText 字段中输入时,它会立即将输入的文本翻译成下面的莫尔斯电码。
问题: 我想要的是扩展 EditTexts 以占用所需的空间,但一旦它们达到一定高度,就可以将其包裹起来。问题是我尝试过的几乎所有事情都导致其中一个 EditText 将另一个移出屏幕或根本不包装其文本。
到目前为止,我得到了什么:
我的布局完全符合我的要求,并且按照我想要的方式工作。它只是看起来不像我想要的那样。我正在使用导航组件,所以下面的视图在 MainActivty 的 FragmentContainerView 中膨胀。我也在设置窗口行为 android:windowSoftInputMode="adjustResize"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/decoder_text_to_morse_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context=".Fragments.DecoderFragment"
>
<androidx.appcompat.widget.AppCompatEditText
android:padding="@dimen/four_dp"
android:gravity="top"
android:id="@+id/include1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="@dimen/sixteen_dp"
android:background="@drawable/card_background"
app:layout_constraintBottom_toTopOf="@+id/decode_changer_fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/sixteen_dp"
android:layout_marginEnd="@dimen/sixteen_dp"
android:text="Text"
app:layout_constraintBottom_toBottomOf="@id/decode_changer_fab"
app:layout_constraintEnd_toStartOf="@+id/decode_changer_fab"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/decode_changer_fab" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/decode_changer_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/eight_dp"
android:src="@drawable/ic_arrow"
app:layout_constraintBottom_toTopOf="@+id/include2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include1" />
<TextView
android:id="@+id/header_morse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/sixteen_dp"
android:layout_marginEnd="@dimen/sixteen_dp"
android:text="Morse"
app:layout_constraintBottom_toBottomOf="@id/decode_changer_fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/decode_changer_fab"
app:layout_constraintTop_toTopOf="@id/decode_changer_fab" />
<androidx.appcompat.widget.AppCompatEditText
android:padding="@dimen/four_dp"
android:gravity="top"
android:id="@+id/include2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="@dimen/sixteen_dp"
android:background="@drawable/card_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/decode_changer_fab" />
</androidx.constraintlayout.widget.ConstraintLayout>
我想要的只是让 EditTexts 以折叠状态开始,展开以容纳它们的内容并在键盘打开时调整大小。使用app:layout_constraintHeight_default="wrap"
不起作用并导致底部的 EditText 将顶部的 EditText 推离屏幕。我意识到app:layout_constraintHeight_default="wrap"
已弃用,推荐的方法是 app:layout_constrainedHeight="true"
withandroid:layout_height="wrap_content"
但行为与底部的 EditText 将顶部的按钮推离屏幕相同。任何帮助将不胜感激。真的很努力做一件平凡的事情-_-
解决方案
对我来说,当我需要这个时,使用最大和最小高度工作,在最大高度之后,编辑文本将变得可滚动
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight= //desired dimensions
android:minHeight = //desired dimensions
/>
推荐阅读
- java - 过滤列表后点击 ListView Item
- jquery - 即使填写了必填字段,Jquery Validate 插件也会引发错误
- laravel - Laravel、Vuejs、axios 获取登录用户的帖子
- android - 改造预期为 BEGIN_OBJECT,但为 STRING
- ios - 是否可以制作包含 SwiftUI 的 Swift 项目模板?
- android - 如何将数据从 Android 中的数据报套接字发送到 Node js 服务器?
- php - 如何在 Laravel 中从 json 索引计数
- node.js - 在 Vagrant 框中连接 Angular 到 Node/express
- python - 单链表 Python 头部和尾部
- java - 首选项屏幕中的自定义列表首选项对话框布局,带有自定义列表项