2012-03-17 9 views
8

पर एक बहुमुखी टेक्स्ट व्यू को रोकें, एक टेक्स्ट व्यू जो कई पंक्तियों तक फैली हुई है, स्वचालित रूप से अधिकतम संभव चौड़ाई फिट करने के लिए विस्तारित होती है। मैं इसे कैसे होने से रोक सकता हूं?अनावश्यक रूप से इसकी अधिकतम चौड़ाई

यहाँ एक उदाहरण लेआउट (test.xml) है

<?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

मैं बाईं तरफ के एक मार्जिन जोड़ देते हैं तो इसे सही ढंग से TextView की चौड़ाई (सामग्री को पुन: फ़ॉर्मेट के बिना) सिकुड़ता है, लेकिन मार्जिन के आकार को टेक्स्टव्यू की सामग्री के अनुसार गणना करना होगा।

<?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

उत्तर

0

उपयोग android:padding="32dp" यह आपकी मदद कर पैडिंग की, यदि आप बाईं उपयोग से अतिरिक्त जगह करना चाहते है android:paddingLeft="32dp"

+1

समस्या पैडिंग के साथ नहीं है। मैं टेक्स्टव्यू को वास्तव में सामग्री को लपेटना चाहता हूं, और इसके बजाय जब यह बहु रेखा है तो यह मूल दृश्य की चौड़ाई तक फैली हुई है। –

7

आप ओवरराइड विधि onMeasure() जहां की गणना के साथ कस्टम TextView उपयोग कर सकते हैं चौड़ाई:

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 :) धन्यवाद! –

0

मैंने Vitaliy Polchuk के समाधान का प्रयास किया है और यह ठीक काम करता है।

... 
@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

आप @Vitaliy Polchuk (एक शीर्ष जवाब) का एक सुझाव उपयोग करना चाहते हैं, तो फिर से लिखने या Why is wrap content in multiple line TextView filling parent? में अपने जवाब देखें::

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; 
    } 
} 

और मैं भी छोटे सुधार जोड़ना चाहते हैं जब आप इस कक्षा को एक्सएमएल में डालते हैं तो एक परियोजना पुनर्निर्माण करें। अन्यथा यह इस वर्ग को नहीं मिलेगा।

संबंधित मुद्दे