我正在嘗試在 android 應用程式中實作材料文本欄位(https://material.io/components/text-fields)的使用。
我目前堅持嘗試為電子郵件的正文實作這一點。它需要是它的 LinearLayout 的高度和寬度,以便它顯示在各種螢屏尺寸上。
我需要解決的問題是輸入的任何文本都被強制放在 TextEdit 的垂直中心而不是頂部的一行上。我不能讓它的高度從一行開始,因為我希望用戶能夠通過單擊大于單行的隱含訊息正文區域中的任何位置來輸入它。
首先,我似乎無法讓 multiLine 功能像一般描述的那樣作業。但是即使可以,我也需要根據可填充的空間(取決于螢屏大小)而不是任意數量的行來設定它的大小。也許我誤解了這將如何作業?
其次,material.io 參考了一個名為 TextArea 的 inputType,它在其他任何地方都沒有被參考,甚至谷歌也沒有幫助發現它。怎么這么隱秘?(https://material.io/components/text-fields#input-types)這是一個可能的解決方案還是只是一個紅鯡魚?
下面提供了代碼,我現在已經在谷歌上搜索了一堆,所以如果有些東西是多余的或者它的格式很奇怪,我很抱歉,但我很感激任何建議來讓它發揮作用或幫助我理解我在做什么錯誤的。
通用文本編輯版本:
<EditText
android:inputType="textMultiLine"
android:singleLine="false"
android:minLines="6"
android:lines="10"
android:maxLines="20"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Message"/>
材料.io版本:
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/noticeBody"
style="@style/DetailNoticeBodyTextView"
android:padding="10dp"
android:inputType="textMultiLine"
android:hint="Message"
android:gravity="top|left"
android:textCursorDrawable="@null">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>
注意:我也嘗試過各種混搭版本,例如。Materials.io 版本中的 minLines/maxLines 等。
uj5u.com熱心網友回復:
有一個晚上在壓力大的作業日程之外仔細考慮它,我意識到我遇到的問題是辨別 com.google.android.material.textfield 和常規 TextEdit 之間的約定的混合。
下面是 com.google.android.material.textfield 的代碼,它完成了我的意圖。
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/noticeBody"
style="@style/DetailNoticeBodyTextView"
android:padding="10dp"
android:inputType="textMultiLine"
android:hint="Message"
android:textCursorDrawable="@null">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|left" />
</com.google.android.material.textfield.TextInputLayout>
與檔案一樣,文本欄位提示、輸入型別、填充等都寫在 TextInputLayout 部分中。這是我在問題中鏈接的檔案的典型約定。事實上,它明確指出應該在那里設定訊息提示以避免意外行為。
然而,重力設定(根據我的經驗)在同一部分沒有影響,需要在 TextInputEditText 部分中設定。我覺得這個“有時-這個-有時-那個”對于 Google 材料來說是一個糟糕的外觀,但我真的不想進入它。盡管如此,這是對我有用的解決方案。
uj5u.com熱心網友回復:
您可以將 設定inputType為textMultiLine。
代碼:
<com.google.android.material.textfield.TextInputLayout
android:id="@ id/noticeBody"
style="@style/DetailNoticeBodyTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:inputType="textMultiLine"
android:hint="Message"
android:gravity="top|left"
android:textCursorDrawable="@null">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="top|start"
android:scrollbars="vertical" >
</com.google.android.material.textfield.TextInputEditText>
</com.google.android.material.textfield.TextInputLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/366564.html
