2015-05-04 10 views
7

TextView होने के कारण, इसकी चौड़ाई अपने माता-पिता की चौड़ाई के 1/3 से अधिक नहीं होनी चाहिए। यदि इसकी चौड़ाई माता-पिता के 1/3 से छोटी है, तो इसमें wrap_content व्यवहार होना चाहिए। इसका क्षैतिज भाई हमेशा इसके आगे शुरू होगा।टेक्स्टव्यू चौड़ाई wrap_content को सेट करने के लिए कैसे करें लेकिन माता-पिता की चौड़ाई 1/12 तक सीमित करें

निम्नलिखित कोशिश की गई, यह हमेशा 1/3 और 2/3 का कड़ी कटौती है, इसलिए यदि टेक्स्ट 1 में 1/3 से कम स्थान है TextView दो इसके आगे शुरू नहीं होंगे।

परिवर्तन LinearLayoutRelativeLayout, तो android:layout_weight="n"

काम नहीं करता है मूल रूप से परिभाषित करने के लिए चौड़ाई wrap_content है की जरूरत है और maxWidth 1/3 से अधिक नहीं है।

कोई सुझाव?

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_weight="2" 
    /> 
</LinearLayout> 

उत्तर

3

मुझे लगता है कि आप गतिशील रूप से माता पिता लेआउट चौड़ाई हर बार जब आप TextView, की तरह कुछ (मैं TextView बदलने के लिए एक बटन और संपादित पाठ का उपयोग कर इस कोड का परीक्षण किया है अद्यतन की जाँच करना होगा - समस्या के बिना काम करता):

<LinearLayout 
    android:id="@+id/myLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tvA" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 

    <TextView 
     android:id="@+id/tvB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 
</LinearLayout> 

कोड:

  TextView tvA = (TextView) findViewById(R.id.tvA); 
      TextView tvB = (TextView) findViewById(R.id.tvB); 
      LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout); 


      // code to use when the textView is updated 
      // possibly button onClickListener? 
      tvA.measure(0, 0); 
      int textWidth = tvA.getMeasuredWidth(); 
      myLayout.measure(0,0); 
      int layoutWidth = myLayout.getWidth(); 

      if (textWidth > (layoutWidth/3)) { 
       tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f)); 
       tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f)); 

      } else { 
       tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
       tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      } 
+0

मैं यह हमेशा 1/3 लेकिन अधिकतम 1/3 होने के लिए नहीं करना चाहते, यह कम से कम 1/3 अनुमति देने के किया जाना चाहिए और भाई उसके बगल में बिना चाहिए अंतरिक्ष। – lannyf

+0

अद्यतन उत्तर देखें ... –

+1

धन्यवाद मार्क! यदि एक्सएमएल में प्राप्त नहीं हो सकता है तो आपका जवाब एक होगा। – lannyf

0

समाधान सरल है: आपकी दूसरी TextView की चौड़ाई पहले एक तरह "0dp" होना चाहिए।

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_weight="2" 
    /> 
</LinearLayout> 
+0

क्षमा करें, मेरे पास एंड्रॉइड है: layout_width = "0dp" और कॉपी त्रुटि नहीं थी। यह काम नहीं करता। – lannyf

0

इसे उस लेआउट में कैसे रखा जाए जिसमें माता-पिता की 1/3 चौड़ाई है?

<LinearLayout 
    .. this is main parent 
    android:weightSum="1" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.33" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</LinearLayout> 
+0

यह माता-पिता के 1/3 को प्राप्त करता है लेकिन इसमें "भाई को सही प्रवाह नहीं होना चाहिए यदि उसके माता-पिता की चौड़ाई के 1/3 कम हो।" – lannyf

3

मूल रूप से परिभाषित करने के लिए चौड़ाई wrap_content है की जरूरत है और maxWidth 1/3 से अधिक नहीं है।

यदि आपको इसकी आवश्यकता है, तो मेरा सुझाव है कि वजन दृष्टिकोण को स्क्रैप करना और गतिशील रूप से टेक्स्ट व्यू के maxWidth मान को सेट करना है।

कुछ इस तरह:

tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3); 
संबंधित मुद्दे