2012-05-16 45 views
5

Tôi có một hoạt động với hai Nút và một TextView trong LinearLayout. TextView của tôi được bù đắp xuống và văn bản không vừa trong hộp. Bạn có thể giải thích những gì đang xảy ra không? Tôi nghĩ rằng nó có liên quan đến padding, và tôi đã đọc một số cuộc thảo luận về các nguy cơ của TextView padding, nhưng điều đó không giải thích tại sao văn bản bị cắt ở phía dưới.Android TextView offset xuống

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="#800080"> 

    <Button 
     android:text="This" 
     android:background="@drawable/button_red" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
    <Button 
     android:text="That" 
     android:background="@drawable/button_green" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
    <TextView 
     android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#533f93" 
    /> 
</LinearLayout> 

Mã này sản xuất màn hình này:

User interface

Các tím là LinearLayout, màu xanh là TextView. Như bạn có thể thấy, đầu của TextView nằm dưới các nút và đáy của TextView nằm dưới đáy LinearLayout. Khi tôi thêm văn bản vào TextView, LinearLayout tăng chiều cao của nó một cách thích hợp, nhưng vì TextView được bù đắp, tôi luôn luôn mất phần cuối của dòng cuối cùng.

Tôi chạy Hierarchy Viewer và nó đã cho tôi wireframe này:

Wireframe image from Hierarchy Viewer

nào cho thấy dọc bù đắp ở phía trên, nhưng nhớ dưới cùng của TextView. Các wireframe tương tự với vẻ LinearLayout chọn như thế này:

Wireframe image from Hierarchy Viewer - LinearLayout selected

Theo Hierarchy Viewer, phía trên cùng của các nút là 0, nhưng phía trên cùng của TextView là 7. Tôi đã thử nhiều bản vá , chủ yếu được chọn từ trang web này:

android:paddingTop="0dp" 
    android:background="@null" 
    android:includeFontPadding="false" 

Không có vấn đề nào trong số này khắc phục được sự cố của tôi.

Trả lời

5

Đặt android:baselineAligned thuộc tính của LinearLayout thành false.

Từ tài liệu:

Khi thiết lập để sai, ngăn ngừa sự bố trí sắp xếp từ đường cơ sở trẻ em của hãng. Thuộc tính này đặc biệt hữu ích khi trẻ sử dụng các giá trị khác nhau cho lực hấp dẫn. Giá trị mặc định là đúng.

+0

Có, tính năng này hoạt động! Tôi không hiểu tại sao mặc định là cho LinearLayout để đẩy văn bản vượt ra ngoài biên giới của nó. – TomDestry

+1

Bạn có thể đọc, ví dụ: bài đăng này để hiểu [baseline.google.com/] có nghĩa là gì (https://groups.google.com/d/topic/android-developers/1gPy9zo28ak/discussion). Vì vậy, theo mặc định, mỗi widget thành công trong 'LinearLayout' được căn chỉnh thẳng hàng với tiện ích con trước đó của nó. Trong trường hợp của chúng ta ** dòng ** đầu tiên của 'TextView' được căn chỉnh thẳng hàng với văn bản của' Nút'.Tôi đồng ý, nó không phải là hành vi trực quan, nhưng nó là. – StenaviN

0

Hãy thử thiết lập các layout_gravity cho TextView như thế này:

<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#533f93" 
    android:layout_gravity="top" 
/> 
+0

Nó sẽ không hoạt động – StenaviN

2

cho layout_gravity của TextView là center_vertical

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:background="#800080"> 

<Button 
    android:text="This" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
<Button 
    android:text="That" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
/> 
<TextView 
    android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this." 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#533f93" 
android:layout_gravity="center_vertical"/> 
</LinearLayout> 
+0

Gah! Vì vậy, dễ dàng, cảm ơn! Mặc định có vẻ là KHÔNG. Bất kỳ ý tưởng tại sao theo mặc định nó đẩy văn bản một phần ra khỏi tầm nhìn? – TomDestry

0

Nếu bạn có vấn đề này với một số TextView bạn có thể thêm android:gravity="center_vertical" đến LinearLayout

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