2011-11-15 27 views
5

Tôi không chắc chắn nó chỉ là "dòng cuối cùng", nhưng chúng tôi có một ứng dụng có một TextView với chiều rộng fill_parent, chiều cao wrap_content. Văn bản đang được đặt trong đó động từ mã Java. Dòng cuối cùng của văn bản chỉ đơn giản là không hiển thị, mặc dù có nhiều không gian trong bố cục. Bên trong của một hệ thống phân cấp khá sâu, do đó, tôi đoán là logic đo lường trong đó đang nhận được điều sai lầm, nhưng nó khá bực bội. Chúng ta cần phải đoán có bao nhiêu dòng sẽ có trong văn bản và thiết lập 'android: lines' cho phù hợp để làm cho nó hoạt động.TextView đã tự động cắt dòng cuối cùng của văn bản

Có ai thấy điều này không? Trong mã, hãy xem id 'contentTextView' về phía dưới cùng. Nếu tôi lấy ra 'android: lines', dòng cuối cùng của văn bản biến mất.

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

    <include android:id="@+id/incHeader" layout="@layout/header"/> 

    <FrameLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

     <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

      <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="101dp" 
        android:background="@drawable/headershadow"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:visibility="invisible" 
        android:layout_weight="1"/> 


      <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="101dp" 
        android:background="@drawable/footershadow"/> 

     </LinearLayout> 

     <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/gybcontentback" 
       android:layout_gravity="center_horizontal" 
       android:orientation="vertical"> 

      <include android:id="@+id/gybHeaderInclude" layout="@layout/gybcontentheader"/> 


       <TextView 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:textColor="#4f4f4f" 
         android:paddingLeft="15dp" 
         android:paddingRight="15dp" 
         android:lines="9" 
         android:id="@+id/contentTextView"/> 

      <ImageView 
        android:layout_marginTop="15dp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/button_blue_rascal_button" 
        android:id="@+id/buttonMoreInfo"/> 

     </LinearLayout> 

    </FrameLayout> 


    <include android:id="@+id/incFooter" layout="@layout/menu"/> 

</LinearLayout> 

Mã Java. Tôi đang điền vào TextView với một chuỗi Java chuẩn trong onCreate.

@Override 
protected String body() 
{ 
    return "Rascal Flatts and The Jason Foundation, Inc. are working together to prevent suicide.\n\n" + 
      "Your Battle Buddy (or family member) may need a friend.\n\n" + 
      "Take the pledge to B1."; 
} 

Các cuộc gọi lớp cơ sở đó để có được những văn bản thực tế

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(contentView()); 
    wireMenu(); 

    TextView headerText = (TextView) findViewById(R.id.gybHeaderText); 
    if(headerText != null) 
     headerText.setText(header()); 

    ((TextView) findViewById(R.id.contentTextView)).setText(body()); // This is where the text is set 
} 

Dù sao, tôi đang sôi nó xuống. Tôi cắt các phần để xem những gì còn lại và vẫn nhận được cùng một vấn đề. Tôi nghĩ rằng tôi có thể đã tìm thấy cò súng, và một giải pháp, nhưng không phải là "nguyên nhân".

+0

Chúng tôi có thể thấy mã Java liên quan đến vấn đề này không? –

+0

Kevin, có thể hữu ích khi nhìn thấy khi bạn đang gọi setText() bc có thể ảnh hưởng đến cách wrap_content thực sự đang vẽ .. – LuxuryMode

Trả lời

1

tôi luộc bố trí xuống đến các bộ phận cơ bản của nó. Đây là phiên bản rút gọn, vẫn đang cắt văn bản:

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


    <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:orientation="vertical"> 

      <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#4f4f4f" 
        android:id="@+id/contentTextView"/> 

      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/button_blue_rascal_button" 
        android:id="@+id/buttonMoreInfo"/> 

     </LinearLayout> 



</LinearLayout> 

Vấn đề, tôi nghĩ, đó là LinearLayout mẹ có chiều rộng từ ImageView bên trong nó. Một nơi nào đó trong hỗn hợp này, TextView đang nhận được một giá trị không chính xác cho chiều cao yêu cầu của nó. Tôi sẽ tưởng tượng điều này đã làm với cách giao diện người dùng được tính toán. Phụ huynh phải yêu cầu trẻ em cho chiều rộng yêu cầu của họ, sau đó quay trở lại và cho tất cả mọi người biết chiều rộng của họ là gì, sau đó yêu cầu chiều cao. Một cái gì đó như thế. Dù sao, các công trình sau đây. Chiều rộng hình ảnh là 263 pixel (tính bằng mdpi), vì vậy nếu tôi đặt 263dp thay vì wrap_content trên bố cục gốc, mọi thứ đều OK.

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


    <LinearLayout 
       android:layout_width="263dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:orientation="vertical"> 

      <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#4f4f4f" 
        android:id="@+id/contentTextView"/> 

      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/button_blue_rascal_button" 
        android:id="@+id/buttonMoreInfo"/> 

     </LinearLayout> 



</LinearLayout> 

Đặt TextView thành wrap_content cũng vậy, mặc dù sau đó đẩy toàn bộ bố cục lớn hơn tôi muốn.

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


    <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:orientation="vertical"> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#4f4f4f" 
        android:id="@+id/contentTextView"/> 

      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/button_blue_rascal_button" 
        android:id="@+id/buttonMoreInfo"/> 

     </LinearLayout> 



</LinearLayout> 

Bạn không chắc chắn về việc mua sắm ở đây là gì. Tóm tắt, nếu bạn bắt đầu thấy các vấn đề về chiều cao được tính toán, hãy cân nhắc sửa chiều rộng trong đó cho phụ huynh, chỉ để các tính toán giao diện người dùng không bị lẫn lộn.

+0

PS. Nếu bạn có một câu trả lời chi tiết hơn giải thích điều này, tôi sẽ bỏ phiếu cho câu trả lời của bạn, mặc dù tôi "trả lời" câu hỏi của riêng tôi;) –

+0

Có một [câu trả lời xuất sắc ở đây] (http://stackoverflow.com/a/11246243/716588) đã giúp giải quyết nó cho tôi. – CodeMonkey

0

Các bạn đã thử

android:singleLine="false" 
+0

Tôi sẽ chụp, nhưng nó hiển thị nhiều hơn một dòng. Chỉ cần không phải là cuối cùng. –

+1

Điều đó không hiệu quả. –

7

Tôi có cùng một vấn đề. Tôi đã tìm được cách giải quyết. Nối dòng mới "\ n" vào văn bản của bạn. Điều này sẽ luôn hiển thị tất cả văn bản của bạn.

+0

Tác phẩm này hoạt động, ngoại trừ khi văn bản chỉ là một dòng bạn kết thúc với một dòng trống bên dưới nó. –

+0

đơn giản và tuyệt vời !!! – dd619

+0

đây là giải pháp đơn giản nhất và đơn giản nhất đối với tôi – u2tall

2

Bạn đã thử xung quanh TextView của mình trực tiếp với LinearLayout, với chiều rộng và chiều cao được đặt thành wrap_content chưa?

Nó làm việc cho tôi ...

+0

Cảm ơn, nó cũng làm việc cho tôi. –

0

Tôi có vấn đề khác nhưng liên quan tương tự. Đây là giải pháp mà tôi nghĩ là phù hợp với những gì bạn cần. Bạn có thể sử dụng trình nghe bố cục toàn cục cho một TextView trong bất kỳ loại ViewGroup nào.

final TextView dSTextView = (TextView)findViewById(R.id.annoyingTextView); 
dSTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
     dSTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this); 

     float lineHeight = dSTextView.getLineHeight(); 
     int maxLines = (int) (dSTextView.getHeight()/lineHeight); 

     if (dSTextView.getLineCount() != maxLines) { 
      dSTextView.setLines(maxLines); 
     } 

    } 
}); 

Bạn có thể đọc thêm về nó here

0

thử thêm

android: đệm

trong bạn TextView. đã làm việc cho tôi

0

Đặt TextView có vấn đề của bạn bên trong FrameLayout (hoặc một số vùng chứa). Tôi nghĩ vấn đề là vì quan điểm anh chị em của bạn.

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