2012-03-17 15 views
8

Birden çok satıra kadar genişleyen bir TextView, olası en yüksek genişliğe sığacak şekilde otomatik olarak genişler. Bunun olmasını nasıl engelleyebilirim?Çok satırlı bir TextView'ın gereksiz genişlikten maksimum genişliğe çıkmasını önleyin

Bu örnek bir düzeni (test.xml) olan

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white"> 
<TextView 
android:background="@color/green" 
android:layout_alignParentRight="true" 
android:id="@+id/foo" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="right" 
android:text="This is the message a much shorter message" 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textColor="@color/black" /> 
</RelativeLayout> 

TextView shown with maximum width

Solda bir marj eklemek, doğru (içeriğinin yeniden olmadan) TextView genişliğini küçülür, Ancak, TextView'ın içeriği dikkate alındığında marjın büyüklüğü hesaplanmalıdır.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white"> 
<TextView 
android:layout_marginLeft="32dp" 
android:background="@color/green" 
android:layout_alignParentRight="true" 
android:id="@+id/foo" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="right" 
android:text="This is the message a much shorter message" 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textColor="@color/black" /> 
</RelativeLayout> 

TextView show with margin

cevap

0

Size sol kullanım dolguyu yapmak istiyorsanız, dolguya sahip olacak şekilde yardımcı olduğunu android:padding="32dp" kullanım android:paddingLeft="32dp"

+1

Sorun sorun değil. TextView'ın içeriği gerçekten sarmasını istiyorum ve bunun yerine çoklu çizgi olduğunda ana görünümün genişliğine genişler. –

7
Sen hesaplamak geçersiz kılınan yöntem onMeasure() ile özel TextView kullanabilir

en:

public class WrapWidthTextView extends TextView { 

... 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    Layout layout = getLayout(); 
    if (layout != null) { 
     int width = (int)FloatMath.ceil(getMaxLineWidth(layout)) 
       + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
     int height = getMeasuredHeight();    
     setMeasuredDimension(width, height); 
    } 
} 

private float getMaxLineWidth(Layout layout) { 
    float max_width = 0.0f; 
    int lines = layout.getLineCount(); 
    for (int i = 0; i < lines; i++) { 
     if (layout.getLineWidth(i) > max_width) { 
      max_width = layout.getLineWidth(i); 
     } 
    } 
    return max_width; 
} 

}

+0

+1000 :) Teşekkürler! –

0

Vitaliy Polchuk çözümünü denedim ve iyi çalışıyor. Eğer @Vitaliy Polchuk (bir üst yanıt) bir öneri kullanmak istiyorsanız

... 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    Layout layout = getLayout(); 
    if (layout != null) { 
     int width = (int) FloatMath.ceil(getMaxLineWidth(layout)) 
       + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 
    } 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    // get already measured dimensions 
    int width = getMeasuredWidth(); 
    int height = getMeasuredHeight(); 
    setMeasuredDimension(width, height); 
} 
... 
0

, bu yüzden yeniden veya Why is wrap content in multiple line TextView filling parent? yaptığı cevaba bakınız:

public class WrapWidthTextView extends AppCompatTextView { 
    public WrapWidthTextView(Context context) { 
     super(context); 
    } 

    public WrapWidthTextView(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public WrapWidthTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     Layout layout = getLayout(); 
     if (layout != null) { 
      int width = (int) Math.ceil(getMaxLineWidth(layout)) 
        + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
      int height = getMeasuredHeight(); 
      setMeasuredDimension(width, height); 
     } 
    } 

    private float getMaxLineWidth(Layout layout) { 
     float max_width = 0.0f; 
     int lines = layout.getLineCount(); 
     for (int i = 0; i < lines; i++) { 
      if (layout.getLineWidth(i) > max_width) { 
       max_width = layout.getLineWidth(i); 
      } 
     } 
     return max_width; 
    } 
} 

Ve ayrıca küçük düzeltme eklemek istediğiniz Bu sınıfı XML'e eklediğinizde projesini yeniden oluşturun. Aksi takdirde bu sınıfı bulamaz.