2015-08-16 6 views
12

मैं एक दृश्य को लेआउट करने की कोशिश कर रहा हूं जो इसकी सामग्री को लपेट ले, लेकिन यह इसकी मूल चौड़ाई से ~ 100dp से कम नहीं होना चाहिए। मैं रिलेटिव लयआउट या कुछ अन्य लेआउट का उपयोग करके ऐसा कैसे कर सकता हूं? मेरे पास अभी क्या है, यह हमेशा अपने माता-पिता से 100 डीपी कम दृश्य बनाएगा ताकि दूसरे दृश्य के लिए जगह हो।अधिकतम चौड़ाई के साथ wrap_content का उपयोग कैसे करें?

enter image description here

आप देख सकते हैं, पाठ पूरे बॉक्स को नहीं भरता है, तो यह छोटे हो सकता है:

इस तस्वीर मैं क्या है का एक उदाहरण है। लेकिन, यह अपने माता-पिता से 100 डीपी से कम कभी नहीं होना चाहिए, ताकि संदेश भेजे जाने के लिए एक कमरा हो।

यह मेरा लेआउट है।

<?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="wrap_content" 
    android:clipChildren="false" 
    android:orientation="vertical" 
    android:paddingBottom="10dp" 
    android:paddingRight="@dimen/horizontalMargin" 
    android:paddingTop="10dp"> 


    <TextView 
     android:id="@+id/message_holder" 
     android:layout_toLeftOf="@id/blank" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/horizontalMargin" 
     android:background="@drawable/message_corners" 
     style="@style/white_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="alsdkjf; alsdkf" /> 

    <RelativeLayout 
     android:id="@+id/blank" 
     android:layout_width="wrap_content" 
     android:layout_height="1dp" 
     android:layout_alignParentRight="true" 
     android:minWidth="100dp"> 

    </RelativeLayout> 

    <TextView 
     android:minWidth="100dp" 
     android:id="@+id/time" 
     style="@style/gray_text" 
     android:layout_toRightOf="@id/message_holder" 
     android:paddingLeft="10dp" 
     android:text="Yesterday,\n11:30 PM" /> 


    <RelativeLayout 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_alignBottom="@id/message_holder" 
     android:layout_alignParentLeft="true" 
     android:background="@drawable/triangle" /> 

</RelativeLayout> 

मैं संदेश बॉक्स के दाईं रिक्ति प्रदान करने के लिए करने के लिए एक खाली दृश्य पर "minWidth" संपत्ति का उपयोग कर की कोशिश की है, लेकिन यह बड़ा (जो संदेश बॉक्स छोटा करने के हैं) होने के लिए आकार परिवर्तन नहीं करता है। जब मेरे पास रिक्त दृश्य नहीं होता है, और बस संदेश बॉक्स के दाईं ओर टेक्स्ट व्यू को समय दें, तो संदेश बॉक्स विस्तारित होने पर टेक्स्टव्यू दिखाई नहीं दे रहा है।

अद्यतन:

यह मेरा "message_corners.xml":

<?xml version="1.0" encoding="utf-8"?> 
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 


    <solid 
     android:color="@color/green" > 
    </solid> 


    <padding 
     android:left="10dp" 
     android:top="10dp" 
     android:right="10dp" 
     android:bottom="10dp" > 
    </padding> 


    <corners 
     android:radius="10dp"> 
    </corners> 

</shape> 

अद्यतन 2:

यह है कि मैं क्या छोटे पाठ के साथ एक लेआउट में खोज रहा है:

enter image description here

और यही वह है जिसे मैं ढूंढ रहा हूं लंबे पाठ के साथ लेआउट:

enter image description here

उत्तर

10

यहां आप एक लेआउट चलाते हैं जो आप चाहते हैं।

<?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="wrap_content" 
    android:clipChildren="false" 
    android:paddingBottom="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="10dp"> 


    <RelativeLayout 
     android:id="@+id/blank" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#aaaaaa"> 
     <LinearLayout 
      android:id="@+id/message_container" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingRight="100dp"> 
      <TextView 
       android:id="@+id/message" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Hello?" 
       android:background="#00ff00" /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/time" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/message_container" 
      android:layout_marginLeft="-100dp" 
      android:text="12:30 PM" 
      android:background="#ff0000" /> 
    </RelativeLayout> 

</RelativeLayout> 

लघु संदेश Short message

लांग संदेश Long message

0

मैं समझता हूँ के रूप में आप 100 डी पी स्क्रीन आकार जो तुम जो किया जाता है पिक्सल में स्क्रीन की चौड़ाई हो रही द्वारा कर सकते हैं की तुलना में कम होने के लिए लेआउट या TextView बनाना चाहते इस

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

से तो फिर तुम textbiew चौड़ाई सेट स्क्रीन आकार आशा है कि यह मदद पीएस मुझे लगता है कि आप पिक्सल के लिए डी पी कन्वर्ट करने के लिए चाहते हो सकता है से कम 100dp हो सकती है, लेकिन मुझे यकीन नहीं कर रहा हूँ

+0

क्या आप थोड़ा और समझा सकते हैं? मैं संदेश बॉक्स पर अधिकतम चौड़ाई सेट करना चाहता हूं, लेकिन मुझे नहीं पता कि वह चौड़ाई क्या है, क्योंकि यह पूरी स्क्रीन भरने के लिए है। –

+0

संदेश_धारक टेक्स्टव्यू –

+0

पर अधिकतम ऊंचाई जोड़ें मुझे लगता है कि वह ऊंचाई – Bhargav

0

आप सही और उसके पर पाठ संदेश छोड़ दिया पर समय पाठ बनाना चाहते हैं, तो आप ऐसा ही कुछ (सापेक्ष लेआउट में इस का उपयोग करते हुए) क्या लेखक क्या कर सकते हैं यह भी आप उपयोग कर सकते maxWidth नहीं minWidth

<TextView 
     android:id="@+id/view_textView_timeText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true"/> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/view_textView_timeText" 
     android:maxWidth="100dp"/> 
5

वास्तव में यह सवाल पूछता है कि टेक्स्टव्यू को उस समय के अंदर संदेश को फिट करने के लिए कैसे टेक्स्टव्यू को ओवरफ्लो किए बिना और खाली रिक्त स्थान छोड़े बिना विस्तारित किया जाए।

चूंकि आप वास्तव में पूरी स्क्रीन की चौड़ाई को नहीं जानते हैं, इसलिए आप अपने टेक्स्ट व्यू को 100 डीपी से कम नहीं बता सकते हैं। आपको अपने टेक्स्ट व्यू को एक कंटेनर में लपेटना चाहिए जिसमें toLeftOf नियम होगा, टेक्स्टव्यू केवल इसकी सामग्री को लपेटकर।इस तरह, कंटेनर सही करने के लिए सभी तरह का विस्तार होगा (समय TextView बह निकला बिना) लेकिन TextView केवल यह पाठ सामग्री (हां, तो यह किसी भी खाली रिक्त स्थान का विस्तार नहीं होगा)

है लपेटो जाएगा कोड

के बजाय

<TextView 
     android:id="@+id/message_holder" 
     android:layout_toLeftOf="@id/blank" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/horizontalMargin" 
     android:background="@drawable/message_corners" 
     style="@style/white_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="alsdkjf; alsdkf" /> 

उपयोग

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"  
android:layout_toLeftOf="@id/blank" 
android:layout_alignParentLeft="true" 
android:layout_marginLeft="@dimen/horizontalMargin"> 

    <TextView 
      android:id="@+id/message_holder" 
      android:background="@drawable/message_corners" 
      style="@style/white_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="alsdkjf; alsdkf" /> 
</LinearLayout> 

वैसे, आपका लेआउट बहुत अच्छा नहीं है। आपको इसे अनुकूलित करना चाहिए।

+0

हम्म, यह सवाल का आधा जवाब है, लेकिन आपको सही समय पर तुरंत दिखाने का समय कैसे मिलेगा? –

+0

उस मामले में, क्षैतिज करने के लिए LinearLayout बदलने संदेश TextView के दाईं तरफ तो यह प्रतीत होता है इसे में समय TextView जोड़ सकते हैं और जब यह पूरी तरह से बढ़ा है LinearLayout एक marginRight 100dp के जोड़ने के लिए, स्थान छोड़ना। – Eran

0

आप क्या कर सकते हैं 2 विचारों के बीच एक खाली दृश्य डालें और इसकी चौड़ाई MATCH_PARENT के रूप में रखें और इस खाली दृश्य को छोड़ने के लिए टेक्स्टव्यू असाइन करें और दिनांक दृश्य के बाईं ओर खाली दृश्य को असाइन करें। बस दृश्य को खाली रखना सुनिश्चित करें।

1

आप विचारों के निम्नलिखित व्यवस्था और उनके चौड़ाई की कोशिश कर सकते हैं:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="6dp" 
     > 
    <FrameLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@android:color/holo_blue_bright" 
       tools:text="Some long test is this which is support to wrap at the end of parent view" 
       /> 
    </FrameLayout> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="6dp" 
      tools:text="Yesterday,\n 11:30 PM" 
      /> 
</LinearLayout> 
+0

इस छोड़कर जब पाठ में कम है, तो समय पहले TextView को फ्लश किया जा रहा है के बजाय सही पर रहता है वास्तव में करीब है। –

0

आप इस तरह कर सकते हैं (नहीं प्रश्न के लिए सीधा जवाब):

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="bottom" 
    android:orientation="horizontal"> 


    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      android:maxLines="1" 
      android:paddingLeft="45px" 
      android:text="asdfadsfsafasdfsakljkljkhjhkkhjkjhjkjhjkhjkhljkhlkhjlkjhljkhljkhlkjhljkhljkhlfasd" 
      android:textColor="#4a4a4a" 
      android:textSize="40px" /> 
    </LinearLayout> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxLines="1" 
     android:paddingLeft="45px" 
     android:paddingRight="48px" 
     android:text="2017.08.09 13:00" 
     android:textColor="#9b9b9b" 
     android:textSize="34px" /> 
</LinearLayout> 
0

मैं जानता हूँ कि यह एक बहुत पुराना सवाल यह है, लेकिन यह एक निराशाजनक समस्या मैं कई बार अब सामना करना पड़ा है और मौजूदा जवाब मैं वह नहीं देख रहा था थे। कुछ सहयोगियों और मैं के साथ आया था निम्नलिखित:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="#FFFFFF"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#888888" 
     android:padding="10dp" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#00FF00" 
      tools:text="Short message."/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="0" 
      android:background="#CCCCCC" 
      tools:text="Yesterday,\n11:30pm"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:background="#888888" 
     android:padding="10dp" 
     android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/message_text_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#00FF00" 
      tools:text="Super ultra mega awesome long message which is going to help us take over the world."/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="0" 
      android:background="#CCCCCC" 
      tools:text="Yesterday,\n11:31pm"/> 

    </LinearLayout> 

</LinearLayout> 

कौन इस तरह दिखता है जब गाया:

enter image description here

जादू पर टेक्स्ट बॉक्स का वजन के लिए शून्य मान हो रहा है दाएं (बाईं ओर दिए गए टेक्स्ट बॉक्स के गैर-शून्य वजन मान के अतिरिक्त, जिसमें से कुछ अन्य उत्तरों में पहले से ही है)।

ईमानदारी से, मैं समझा नहीं सकता कि यह क्यों काम करता है, लेकिन इतने लंबे समय तक इसका समाधान ढूंढने के बाद मैं इस पर सवाल नहीं उठा रहा हूं! :)

एक के रूप में अलग रूप में, मैं इस दृष्टिकोण की तरह है क्योंकि यह दृश्य ओवरले लागू करने के लिए किसी भी स्पष्ट या न्यूनतम चौड़ाई, किसी भी मध्यवर्ती आवरण दृश्य, या कतरन सेटिंग्स, मार्जिन, पैडिंग, आदि के उपयोग की आवश्यकता नहीं है।

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