2012-09-21 35 views
48

Dưới đây là cách bố trí của tôi:Làm thế nào để EditText Box Chiều cao Mở rộng

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/etTweetReview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:ems="10" 
     android:hint="Discuss up to 140 characters" 
     android:imeOptions="actionDone" 
     android:inputType="textCapSentences" 
     android:lines="2" 
     android:maxLength="140" 
     android:paddingBottom="10dp" 
     android:singleLine="false" /> 

    <Button 
     android:id="@+id/theReviewBarButton" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="5dp" 
     android:text="Submit Review" 
     android:textSize="22sp" /> 

    <Spinner 
     android:id="@+id/spinnerSort" 
     android:layout_width="120dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" /> 

    <ListView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/android:list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="5dp" 
     android:scrollbars="none" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="8dp" > 
    </ListView> 

</LinearLayout> 

Khi đó hộp EditText được đến cuối cùng, tôi muốn nó quấn vào dòng tiếp theo. Ngay bây giờ, nó chỉ tiếp tục di chuyển sang phải mọi thứ rời khỏi màn hình.

+1

Bản sao có thể có của [văn bản EditText của Android Word-Wrap] (http://stackoverflow.com/questions/3276380/android-word-wrap-edittext-text) – blahdiblah

Trả lời

97

Đã được hỏi và trả lời trước đó.

Android Word-Wrap EditText text

Ví dụ mã XML:

<EditText 
    android:id="@+id/edtInput" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:hint="@string/compose_hint" 
    android:inputType="textCapSentences|textMultiLine" 
    android:maxLength="2000" 
    android:maxLines="4" /> 
+7

Tôi đã xem các câu trả lời khác nhưng bỏ lỡ điều lớn nhất: 'textMultiLine' cảm ơn, đánh dấu chính xác. – KickingLettuce

+0

android: layout_height = "wrap_content" android: inputType = "textCapSentences | textMultiLine" điều này sẽ làm cho công việc –

+0

Thuộc tính tuyệt vời mà tôi không biết. Suy nghĩ để cập nhật các maxlines động dựa trên số chuỗi. –

25

Giả sử rằng bạn chỉ muốn có một dòng lúc đầu sau đó mở rộng nó để 5 dòng và bạn muốn có tối đa 10 dòng.

<EditText 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/etMessageBox" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:autoLink="all" 
        android:hint="@string/message_edit_text_hint" 
        android:lines="5" 
        android:minLines="1" 
        android:gravity="top|left" 
        android:maxLines="10" 
        android:scrollbars="none" 
        android:inputType="textMultiLine|textCapSentences"/> 

Bằng cách tăng android:lines bạn có thể xác định mở rộng số lượng dòng.

+0

Điều gì sẽ xảy ra nếu chúng ta muốn bắt đầu với 4 dòng và có văn bản cuộn lên khi người dùng đã nhập nhiều hơn 4 dòng văn bản? – Ivan

3

thử mã này

<EditText 
    android:id="@+id/"edittext01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="textCapSentences|textMultiLine" 
    android:maxLength="100" 
    android:maxLines="10"/> 
+1

Nó sẽ thực sự tốt nếu bạn có thể thêm một số bình luận để thêm ngữ cảnh cho câu trả lời này. –

0

Hủy bỏ dòng này hình thành EditText bạn

android:inputType="textCapSentences" 

và thêm dòng này với số dòng theo yêu cầu của bạn

android:lines="2" 

Hy vọng điều này sẽ giúp bạn

3

Chỉ cần thêm android: lines = "6" để EditText bạn

<Edittext 
     android:id="@+id/edt_address" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" 
     android:maxLines="6"    
     android:lines="6" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:gravity="left"/> 
0

Bạn cần chiều cao EditText bạn thiết lập để android:layout_height="wrap_content"

&

android:lines="5" 

này đã làm các trick cho tôi bên ConstraintLayout

Các vấn đề liên quan