5

तो मुझे ListView में दो TextViews प्रति पंक्ति मिली है। एक छोड़ा जाना चाहिए और दूसरा अधिकार गठबंधन किया जाना चाहिए। TextViews दोनों में पृष्ठभूमि के रूप में एक गोलाकार आयताकार है जो केवल पाठ को अंदर लपेटना चाहिए। तो मैं इस के साथ आया था:दो टेक्स्टव्यू को संरेखित करें, पृष्ठभूमि को खींचने के बिना सूची दृश्य पर एक अन्य दाईं ओर छोड़ दिया गया है

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/text_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/bubble_purple" 
     android:gravity="center" > 
    </TextView> 

    <TextView 
     android:id="@+id/text_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@id/text_right" 
     android:background="@drawable/bubble_blue" 
     android:gravity="center" > 
    </TextView> 
</RelativeLayout> 

यह लंबे समय से ग्रंथों के लिए अच्छा लग रहा है लेकिन छोटे संदेशों के लिए नहीं:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/text_left" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:background="@drawable/bubble_blue" 
     android:gravity="center" > 
    </TextView> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <TextView 
     android:id="@+id/text_right" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:background="@drawable/bubble_purple" 
     android:gravity="center"> 
    </TextView> 
</LinearLayout> 

:

enter image description here

मैं भी इस तरह एक LinearLayout की कोशिश की यह छोटे संदेशों के लिए काम करता है लेकिन लंबे समय तक नहीं:

enter image description here

क्या TextViews की संयुक्त चौड़ाई को मापने और इन लेआउट के बीच प्रोग्रामेटिक रूप से स्विच करने के लिए किसी भी तरह से संभव है, या क्या मैं यहां कुछ गलत कर रहा हूं?

+1

कुछ लेआउट वजन –

+0

कोशिश एंड्रॉयड रखो: दोनों – Android2390

+0

के लिए layout_weight = "1" कैसे दो 'खिंचाव TextViews' चाहिए अगर पहले एक छोटा है और दूसरा बड़ा है? – Luksprog

उत्तर

6

क्या यह काम आप चाहते हैं?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/text_right" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="@drawable/bubble_blue" 
    android:text="2" 
    android:gravity="center" > 
</TextView> 

<TextView 
    android:id="@+id/text_left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="@drawable/bubble_purple" 
    android:text="9" 
    android:gravity="center" > 
</TextView> 
</LinearLayout> 

layout_weight पैरामीटर जोड़ने और उन्हें बराबर बनाकर, आप दो विचारों के बीच समान रूप से किसी भी अतिरिक्त कमरे आवंटित करने के लिए लेआउट इंजन बताओ। यदि आप बाएं ब्लॉक को दाएं से बड़े होने की कामना करते हैं, तो आप इसे बड़ा वजन दे सकते हैं।

संपादित करें: हो सकता है कि यह है बेहतर:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<LinearLayout 
    android:id="@+id/text_left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#456" 
     android:text="5 + 1" 
     android:gravity="center" > 
    </TextView> 

</LinearLayout> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text=" = " > 
</TextView> 

<LinearLayout 
    android:id="@+id/text_right" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layoutDirection="rtl" 
    android:layout_weight="0.5"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#856" 
      android:text="6" 
      android:gravity="center" > 
     </TextView> 

</LinearLayout> 

</LinearLayout> 
+0

नहीं, बिल्कुल नहीं। निश्चित रूप से यह लंबे ग्रंथों के लिए काम करता है लेकिन छोटे ग्रंथों के लिए यह [पृष्ठभूमि को फैलाता है] (http://img225.imageshack.us/img225/8269/linearstretchedbg.png)। आदर्श रूप में, पृष्ठभूमि को केवल पाठ को लपेटना चाहिए और ऊपर की दूसरी छवि में जैसे छोटे संदेशों के लिए बुलबुले के बीच स्थान होना चाहिए। – wollan

+0

अच्छा! उन्हें 'व्यू ग्रुप' में लपेटकर, कभी सोचा नहीं। हालांकि, मुझे 'एंड्रॉइड: लेआउट डायरेक्शन 'के लिए" कोई संसाधन पहचानकर्ता नहीं मिला "मिला। लेकिन दूसरे आंतरिक 'लीनियरलाउट' को 'फ्रेमलेआउट' पर स्विच करना और 'एंड्रॉइड: लेआउट_ग्रैविटी = "दाएं" ने चाल बनाई। धन्यवाद! – wollan