2016-08-09 13 views
9

मैं स्क्रीन (फिक्स्ड-आकार वाले चित्र और 2 सिंगल लाइन पाठ विचार: leftTextView और rightTextView) पर क्षैतिज बिछाने कर रहा हूँ 3 बार देखा गया है और मैं के खिलाफ गले लगाना rightTextView पाने के लिए कोशिश कर रहा हूँ leftTextView, लेकिन इस स्थिति में कि दोनों लेबल की चौड़ाई स्क्रीन आकार से अधिक हो जाएगी, leftTextiew को छोटा करें।एंड्रॉयड TextView ट्रंकेशन प्राथमिकता या संपीड़न प्रतिरोध

वांछित कार्यशीलता का उदाहरण:

|img|leftText|rightText|    ||(end of screen) 
|img|leftTextMedium|rightText|   || 
|img|leftTextTooLongSoTrunc...|rightText|| 

क्या वास्तव में हो रहा है:

|img|leftText|rightText|    ||(end of screen) 
|img|leftTextPrettyLongButNotHuge|rightT|| 
|img|leftTextWhichIsIncrediblyLongBlahBl|| 

वर्तमान में, यदि दोनों पाठ विचारों के आकार को देखने की चौड़ाई से अधिक है, rightTextView समाप्त होता है बनाने के लिए नीचे squished हो रही कमरा, भले ही मैंने android:ellipsize="end" को leftTextView पर सेट किया है।

क्या leftTextView को "छंटनी प्राथमिकता" बनाने का कोई तरीका है जो कहता है कि अगर इसे अन्य विचारों को फिट नहीं किया जाए तो इसे छोटा कर दिया जाना चाहिए? या दूसरी तरफ, क्या मैं को leftTextView के लिए जगह बनाने के लिए स्क्विश होने से रोकने के लिए "संपीड़न प्रतिरोध" सेट कर सकता हूं?

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

    <ImageView 
     android:id="@+id/fixedWidthImage" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:id="@+id/leftTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toEndOf="@+id/fixedWidthImage" 
     android:layout_toRightOf="@id/fixedWidthImage" 
     android:maxLines="1" 
     android:textSize="18sp" 
     android:ellipsize="end" /> 

    <TextView 
     android:id="@+id/rightTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toEndOf="@+id/leftTextView" 
     android:layout_toRightOf="@+id/leftTextView" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentRight="true" 
     android:maxLines="1" 
     android:textSize="12sp" /> 

</RelativeLayout> 

बातें मैं कोशिश की है: यहाँ मेरी एक्सएमएल का नमूना दिया गया

  1. leftTextView में, जोड़ने के एक toStartOf/toLeftOfrightTextView (अनुप्रयोग क्रैश हो)
  2. के बजाय संरेखित rightTextViewleftTextView के अंत/दाएं भाग में, इसे फ्लिप करें और के प्रारंभ/बाएं leftTextView को संरेखित करें। इसके परिणामस्वरूप rightTextView को leftTextView के दाईं ओर सीधे स्क्रीन के अंत तक लांच किया जा रहा है।

    |img|leftText     |rightText|| 
    |img|leftTextMedium   |rightText|| 
    |img|leftTextTooLongSoTrunc...|rightText|| 
    
  3. यहां तक ​​कि अगर मैं android:minWidth="25dp" बस सुनिश्चित करने के लिए कम से कम हिस्सा rightTextView की दिख रहा है (जो मुझे क्या करना है क्योंकि इस पाठ की लंबाई चर रहा है नहीं करना चाहती) सेट करें: यह कैसे समाप्त होता है देखने का उदाहरण यह अभी भी leftTextView के लिए जगह बनाने के लिए चौड़ाई को संपीड़ित कर देता है।

  4. प्रति Emanuel Moecklin का उत्तर, मैंने LinearLayout में लपेटने की कोशिश की और leftTextView पर वजन निर्धारित किया। नतीजा यह था कि मैंने # 2 में क्या प्रयास किया था।
+0

डुप्लिकेट अंकन के अपने बचाव में, कि अन्य प्रश्न बार मैं मेरा पूछा पर एक जवाब नहीं था। इसके अलावा, इसका शीर्षक भयानक है, शायद यही कारण है कि मुझे यह नहीं पता था कि यह पहले स्थान पर मौजूद था। – Stonz2

उत्तर

6

आप इस लेआउट को TableLayout और shrinkColumns विशेषता से संग्रहीत कर सकते हैं।

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

    <!-- Row 1 --> 
    <TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:shrinkColumns="1"> 

     <TableRow 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical"> 

      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@mipmap/ic_launcher"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:maxLines="1" 
        android:ellipsize="end" 
        android:text="leftText"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:maxLines="1" 
        android:ellipsize="none" 
        android:text="rightText"/> 
     </TableRow> 

    </TableLayout> 


    <!-- Row 2 --> 
    <TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:shrinkColumns="1"> 

     <TableRow 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical"> 

      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@mipmap/ic_launcher"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:maxLines="1" 
        android:ellipsize="end" 
        android:text="leftTextPrettyLongButNotHuge"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:maxLines="1" 
        android:ellipsize="none" 
        android:text="rightText"/> 
     </TableRow> 

    </TableLayout> 

    <!-- Row 3 --> 
    <TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:shrinkColumns="1"> 

     <TableRow 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center_vertical"> 

      <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@mipmap/ic_launcher"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:maxLines="1" 
        android:ellipsize="end" 
        android:text="leftTextWhichIsIncrediblyLongBlahBlahBlahBlah"/> 

      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:padding="4dp" 
        android:maxLines="1" 
        android:ellipsize="none" 
        android:text="rightText"/> 
     </TableRow> 

    </TableLayout> 

</LinearLayout> 

enter image description here

+0

क्या यह लंबवत लेआउट के लिए किया जा सकता है? मुझे टेबलरो में आइटम्स को लंबवत रूप से गठबंधन करने और एंड्रॉइड सेट करने की आवश्यकता है: पहले दृश्य को कम करने के लिए कॉलम = "0" को संक्षिप्त करें। – Vlad

1

यहाँ मैं क्या करना है:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/fixedWidthImage" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:src="@drawable/ic_launcher_pro" /> 

    <TextView 
     android:id="@+id/leftTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center_vertical|left" 
     android:ellipsize="marquee" 
     android:lines="1" 
     android:text="This is a very long text, and now even longer" /> 

    <TextView 
     android:id="@+id/rightTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|left" 
     android:lines="1" 
     android:text="This is also quite long" /> 

</LinearLayout> 

एंड्रॉयड: layout_weight = "चाल" यहाँ "1" करता है। ,

उदाहरण के लिए अगर वहाँ तीन पाठ क्षेत्र हैं और उनमें से दो, 1 की एक वजन घोषित, जबकि अन्य कोई वजन दिया जाता है, तीसरे पाठ क्षेत्र: प्रलेखन यह एक यद्यपि पर काफी स्पष्ट नहीं है बिना वजन के बढ़ेगा और केवल द्वारा आवश्यक सामग्री पर कब्जा कर लिया जाएगा।

(https://developer.android.com/guide/topics/ui/layout/linear.html#Weight)

यह मामला है जिसमें वहाँ भरने के लिए अंतरिक्ष लेकिन सभी विचारों के लिए पर्याप्त नहीं स्थान नहीं है पर विस्तृत नहीं है। दस्तावेज के उल्लिखित टुकड़े की एक व्याख्या यह होगी कि वजन के बिना एक क्षेत्र (इस मामले में दाएं पाठ दृश्य) अपनी सामग्री के अनुसार आवश्यक क्षेत्र पर कब्जा कर लेगा, जबकि वजन वाले क्षेत्र (इस मामले में बाएं टेक्स्टव्यू) "बढ़ेगा" (नकारात्मक में विकास) और यह वही है जो होता है।

+0

जबकि वज़न आपके द्वारा उल्लेख किए गए "नकारात्मक विकास" के लिए अनुमति देता है, यह चौड़ाई को भरने के लिए फ़ील्ड को भी विस्तारित करता है जब इसकी आवश्यकता नहीं होती है। यह 'दाएं पाठ दृश्य' के साथ समाप्त होता है जो मूल दृश्य के दाएं/अंत में पिन किया जाता है। कार्यात्मक रूप से, यह वही दिखाई देता है जैसा कि मैंने प्रश्न में किए गए # 2 में किया है। – Stonz2

1

मुझे लगता है कि आपको कस्टम लेआउट बनाने की आवश्यकता होगी। यह मेरे द्वारा बनाए गए नमूने के लिए अच्छी तरह से काम करता है और यह आपके द्वारा ऊपर पोस्ट की गई सभी आवश्यकताओं को पूरा करता है, लेकिन आपको layout_margins और पैडिंग के लिए कोड को अधिक गणित जोड़ने की आवश्यकता होगी।

यहां क्या हो रहा है इसका मूल विचार यह है कि मैंने लीनियरलाउट को उप-वर्गीकृत किया है और ऑनमेजर पास में, मैं यह सुनिश्चित करने के लिए जांच करता हूं कि सही दृश्य में जितना संभव हो उतना स्थान लेना है। ध्यान दें कि मुझे सही दृश्य को पुनः प्राप्त करना है और न केवल मापा चौड़ाई प्राप्त करें क्योंकि लाइनरलेआउट पर्याप्त जगह नहीं दे रहा है। एक बार जब आपके पास सही दृश्य कितना कमरा लगता है, तो यह देखने के लिए जांचें कि क्या मध्य दृश्य (बाएं व्यू ... माफ करना कहा जाता है) आपके इच्छित स्थान से अधिक स्थान ले रहा है। अगर ऐसा होता है, तो बस इसे छोड़ दें।

रीसाइक्लिंग व्यू में होने की आपकी आवश्यकताओं के आधार पर ... मुझे पता है कि आपने कहा था कि आप एक शुद्ध एक्सएमएल समाधान चाहते हैं, लेकिन यदि आप इसे कोड में एक्सएमएल 1 से निपटते हैं तो बेहतर प्रदर्शन प्राप्त करने जा रहे हैं। असंभव हो सकता है और 2. इसे स्वयं सीधे करने से अधिक कंप्यूटेशंस की आवश्यकता होगी।

public class CustomLinearLayout extends LinearLayout { 

private View child0; 
private TextView leftView, rightView; 
public CustomLinearLayout(Context context) { 
    super(context); 
} 

public CustomLinearLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (rightView == null) { 
     child0 = getChildAt(0); 
     leftView = (TextView)getChildAt(1); 
     rightView = (TextView) getChildAt(2); 
    } 

    int spec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.AT_MOST); 
    rightView.measure(spec, spec); 

    int remaining = getMeasuredWidth() - child0.getMeasuredWidth() - rightView.getMeasuredWidth(); 
    if (leftView.getMeasuredWidth() > remaining) { 
     int specW = MeasureSpec.makeMeasureSpec(remaining, MeasureSpec.AT_MOST); 
     int specH = MeasureSpec.makeMeasureSpec(leftView.getMeasuredHeight(), MeasureSpec.EXACTLY); 
     leftView.measure(specW, specH); 
    } 
} 

एक्सएमएल लेआउट

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context="com.example.joshua.myapplication.MainActivity"> 

<com.example.joshua.myapplication.CustomLinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <ImageView 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:background="#FF0000"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:textSize="18sp" 
     android:text="My Left View With Really Really Long Text That should fill the screen"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:textSize="18sp" 
     android:text="My Right View" 
     /> 
</com.example.joshua.myapplication.CustomLinearLayout> 

<com.example.joshua.myapplication.CustomLinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_marginTop="20dp"> 
    <ImageView 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:background="#FF0000"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:maxLines="1" 
     android:textSize="18sp" 
     android:text="My Left View with short text"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:textSize="18sp" 
     android:text="My Right View" 
     /> 
</com.example.joshua.myapplication.CustomLinearLayout> 
</LinearLayout> 

enter image description here

+0

उत्तर के लिए धन्यवाद- मैंने इसे एक भंवर दिया और यह बहुत अच्छा काम करता है, लेकिन नश्मुरा का दृष्टिकोण भी काम करता है, लेकिन पूरी तरह से एक्सएमएल में है और यह सबक्लासिंग पर भरोसा नहीं करता है। – Stonz2

0

बस अपनी बाईं TextView को android:maxWidth संपत्ति का उपयोग करें। तो, आपका बायांव्यू उस सीमा से अधिक कभी नहीं होगा। और आपके सही दृश्य के लिए हमेशा जगह होगी।

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

    <ImageView 
     android:id="@+id/fixedWidthImage" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/leftTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toEndOf="@+id/fixedWidthImage" 
     android:layout_toRightOf="@+id/fixedWidthImage" 
     android:ellipsize="end" 
     android:lines="1" 
     android:maxWidth="250dp" 
     android:text="This is a Long left Text which never ends and You'll be tired after reading this long stuff.. blah blah... " /> 

    <TextView 
     android:id="@+id/rightTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toEndOf="@+id/leftTextView" 
     android:layout_toRightOf="@+id/leftTextView" 
     android:ellipsize="end" 
     android:lines="1" 
     android:text="Right Text" /> 

</RelativeLayout> 

Preview of layout

हालांकि, समाधान के साथ सीमा आप maxWidth संपत्ति में निश्चित मूल्य देने के लिए है। तो, यह अलग-अलग स्क्रीन आकार पर अलग-अलग रूप प्रदर्शित करेगा। हालांकि, ज्यादातर मामलों में आपको समस्या का सामना नहीं करना पड़ेगा।

इसके अलावा, एकल टेक्स्ट android:ellipsize="marquee" में लंबे टेक्स्ट व्यू को प्रदर्शित करने के लिए अच्छा विकल्प है। क्योंकि यह आपके दृश्य क्षैतिज स्क्रॉल करेगा। लेकिन इसे बनाने के लिए अपने कोड में textView.setSelected(true) लिखना न भूलें। Detail

+0

एंड्रॉइड के साथ स्क्रीन आकारों (प्लस पोर्ट्रेट और लैंडस्केप) की इतनी व्यापक असमानता है कि निश्चित चौड़ाई निश्चित रूप से एक वांछनीय दृष्टिकोण नहीं है। – Stonz2

+0

मैंने पहले से ही कहा है कि यह काम करेगा जब आप कुछ निश्चित चौड़ाई के आकार को जानते हैं। अन्यथा आप प्रोग्रामिंग रूप से @whizzle के रूप में सुझाव दे सकते हैं। –

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