2013-04-09 24 views
6

Có, đây là một câu hỏi phát triển edittext khác trên SO. Và vâng tôi đã trải qua tất cả những câu hỏi phát triển edittext. Nhưng cái này hơi cụ thể.Android edittext tự động phát triển với văn bản và đẩy mọi thứ xuống

UI:

enter image description here

Một cách bố trí đơn giản với android EditText và nút bên dưới nó. Khi văn bản ít hơn, giao diện edittext vẫn bao phủ toàn bộ màn hình để lại khoảng trống cho nút bên dưới.

Khi người dùng bắt đầu nhập văn bản, edittext sẽ phát triển theo chiều dọc để phù hợp với văn bản. Đồng thời, nút cũng nên tiếp tục đẩy xuống để cho edittext phát triển.

Người dùng có thể cuộn dọc qua màn hình để xem toàn văn.

UI sau khi nhập văn bản quan trọng:

enter image description here

Dưới đây là cách bố trí với EditText và nút bên dưới:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/background" 
    android:clipToPadding="false" 
    android:fadingEdge="none" 
    android:fillViewport="true" 
    android:padding="13dp" 
    android:scrollbarStyle="outsideOverlay" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1"> 

     <!-- some fixed width image --> 
     <ImageView 
      android:id="@+id/someimage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="4dp" 
      android:src="@drawable/page_white_text" /> 

     <EditText 
      android:id="@+id/some" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/someimage" 
      android:background="@null" 
      android:gravity="top" 
      android:minLines="10" 
      android:minHeight="50dp" 
      android:textColor="#222222" 
      android:textSize="15dp" 
      android:typeface="serif" > 
      <requestFocus /> 
     </EditText> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:baselineAligned="true" 
      android:layout_marginTop="5dp" 
      android:padding="10dp" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

     </LinearLayout> 
    </RelativeLayout> 

</ScrollView> 

này hoạt động nhiều hơn hoặc ít hơn. Nhưng khi tôi nhập nhiều văn bản, thay vì nhấn nút xuống dưới. Nó đi đằng sau nó thích:

enter image description here

Bất kỳ trợ giúp ở đây được nhiều đánh giá cao. Cảm ơn :)

Trả lời

2

Dưới đây là khuyến nghị của tôi: bên trong scrollview, đặt một RelativeLayout trong đó bạn có thể đặt tất cả các phần tử trong một trật tự nhất định sử dụng android: layout_below. Để tự động phát triển EditText của bạn, hãy sử dụng số solution này.

Chúng ta hãy xem một ví dụ:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="center"> 
<RelativeLayout 
    android:id="@+id/general2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_margin="20sp" > 

    <TextView 
     android:id="@+id/textFirstOpinion" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="@string/titleFirstOpinion" 
     android:textStyle="bold" 
     android:textSize="23sp" 
     android:textColor="#FFFFFF" 
     android:layout_marginTop="10dp" /> 

    <EditText 
     android:id="@+id/fieldFirstOpinion" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/textFirstOpinion" 
     android:minHeight="80sp" 
     android:isScrollContainer="true" 
     android:inputType="textMultiLine" 
     android:textSize="20sp" 
     android:gravity="top|left" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="110dp" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/fieldFirstOpinion" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="37sp" 
     android:textSize="23sp" 
     android:text="@string/textSendButton" 
     android:gravity="center_horizontal" 
     android:padding="10dp" /> 

</RelativeLayout> 
</ScrollView> 
0

thử thêm android: layout_below = "@ id/some" trong LinearLayout của bạn như

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:baselineAligned="true" 
     android:layout_marginTop="5dp" 
     android:padding="10dp" 
     android:orientation="vertical" 
     android:layout_below="@id/some" > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Button" /> 

    </LinearLayout> 
+0

Hi Vinit, vui lòng xem nhận xét về trên ans. –

0

Vì bạn muốn nút là ở dưới cùng của trang, thêm dòng sau

android:layout_below="@+id/some" 
android:layout_centerHorizontal="true" 
android:layout_alignParentBottom="true" 

trong mã LinearLayout của bạn ở đây như

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/some" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:baselineAligned="true" 
     android:layout_marginTop="5dp" 
     android:padding="10dp" 
     android:orientation="vertical" > 

Ngoài ra hãy chắc chắn rằng layout:height tham số của EditText được thiết lập để wrap_content Hãy thử điều này và cho tôi biết nếu nó hoạt động hay không

+0

Cài đặt layout_below thành id/một số làm cho nút gắn thành edittext. Tôi muốn edittext được hiển thị toàn màn hình và nút để dính ở phía dưới. Nếu văn bản trong văn bản chỉnh sửa phát triển vượt ra ngoài màn hình, văn bản sẽ đẩy nút bên dưới. Điều này hoạt động một phần: ( –

+0

okay tôi đang chỉnh sửa câu trả lời của mình –

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