英文:
Android - How to make the height of a TextInputEditText to be exactly of 2 lines?
问题
我有一个像这样的布局:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/user_description_input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:hint="描述"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:startIconContentDescription="Lalala">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/user_description_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="描述" />
</com.google.android.material.textfield.TextInputLayout>
它的高度正好是一行的大小,但我希望它可以正好是2行的大小。
我尝试在我的TextInputEditText
标签中添加以下属性:
<com.google.android.material.textfield.TextInputEditText
...
android:maxLines="2"
android:scrollbars="vertical" />
但这使它以1行高度开始,当用户在上面键入时停在2行。我希望它从一开始就固定为2行,即使没有足够的文本需要2行。
我还希望它具有固定的大小,并允许用户在添加大于2行的文本时垂直滚动。
我知道我可以通过添加足够的字符使其具有2行高度,然后固定这个高度大小,然后清除TextInputEditText
来以编程方式实现,但那是一个非常丑陋的解决方案。
英文:
I have a layout like this:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/user_description_input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="16dp"
android:hint="Description"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:startIconContentDescription="Lalala">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/user_description_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="description" />
</com.google.android.material.textfield.TextInputLayout>
It's height is exactly the size of ONE line, but I wish it could be the size of 2 line exactly.
I tried adding theses attributes to my TextInputEditText
tag:
<com.google.android.material.textfield.TextInputEditText
...
android:maxLines="4"
android:scrollbars="vertical" />
But that made it to start as a 1 line height and stop at 2 as the user types on it. I would like it to be fixed on 2 from the begining, even if it did not have enough text to need 2 lines.
I also would like it to have a fixed size and allow the user to scroll vertically in case he add some text that is larger than 2 lines.
I know I COULD do it programatically by adding enough caracters until it has a 2 lines height and fix this heigh size and then clean the TextInputEditText
, but that is such an ugly solution.
答案1
得分: 3
尝试这样做,在你的 TextInputEditText
中:
android:minLines="2"
android:gravity="top"
英文:
Try this, in your TextInputEditText
android:minLines="2"
android:gravity="top"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论