在 Android 開發中,當我們設定 EditText 為多行輸入(android:singleLine="false"、android:lines="5")並加上 android:gravity="center" 時,文字區域雖然外觀正常,但游標卻會閃爍在區域的正中央,這違反了使用者直覺——游標應該出現在第一行的起始位置。
只需將 android:gravity 從 center 改為 top 即可:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:singleLine="false"
android:lines="5"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
gravity="center":將內容(含游標)置於元件中心,導致游標初始位置偏移。gravity="top":將內容靠上對齊,游標自然落在第一行行首,符合輸入習慣。若同時需要水平置中與垂直靠上,可使用 android:gravity="top|center_horizontal"。此外,android:lines 僅設定最小行數,實際高度仍由內容決定;若需固定高度,請改用 android:minLines 與 android:maxLines 搭配。