Android लेआउट वजन


84

मैं एंड्रॉइड विकास के लिए नया हूं और मेरे पास रैखिक लेआउट में वजन सेट करने के बारे में एक सवाल है।

मैं दो कस्टम बटन और एक कस्टम संपादित पाठ के साथ एक पंक्ति बनाने की कोशिश कर रहा हूं। संपादित पाठ को केवल अपनी सामग्री के रूप में अधिक से अधिक कमरा लेना चाहिए, और पंक्ति में उपलब्ध स्थान के बाकी हिस्सों को भरने के लिए दो बटन क्षैतिज रूप से विस्तार करना चाहिए। इस कदर...

| [#Button++#] [#Button--#] [et] |

कई प्रयासों के बाद यह निकटतम है जो मुझे चाहिए, भले ही यह अत्यधिक जटिल लगता है।

| [Button] [Button] [###ET###] |

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center_vertical"
    android:layout_weight="1"
    >

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <RelativeLayout 
            android:id="@+id/btn_plus_container" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            >
            <ImageButton 
                android:layout_height="wrap_content"
                android:layout_width="fill_parent" 
                android:id="@+id/btn_plus" 
                android:background="@drawable/btn"
                android:layout_centerInParent="true"
                ></ImageButton>
            <TextView 
                android:textColor="#FAFAF4"
                android:id="@+id/tv_dice_plus" 
                android:text="@string/tv_plus" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                ></TextView>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <RelativeLayout 
            android:id="@+id/btn_minus_container" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            >
            <ImageButton 
                android:layout_height="wrap_content"
                android:layout_width="fill_parent" 
                android:id="@+id/btn_minus" 
                android:background="@drawable/btn"
                android:layout_centerInParent="true"
                ></ImageButton>
            <TextView 
                android:textColor="#FAFAF4"
                android:id="@+id/btn_minus" 
                android:text="@string/tv_minus" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                ></TextView>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <EditText 
            android:textColor="#FAFAF4"
            android:text="@string/et_output" 
            android:id="@+id/et_output" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:inputType="number"
            android:selectAllOnFocus="true" 
            android:minWidth="50sp"         
            android:maxWidth="50sp"
            android:background="@drawable/tv_black_background3"
            android:textColorHighlight="#c30505"
            ></EditText>
    </LinearLayout>
</LinearLayout>

जैसा कि मैं समझता हूं कि सेटिंग [रैखिक: लेआउट_वेट = "2"] बटन को पकड़े हुए रैखिक लेआउट के लिए उन्हें संपादित पाठ की तुलना में अधिक जगह लेनी चाहिए, लेकिन यह मेरे लिए विपरीत है, जिससे दोनों आधे आकार में बन जाते हैं।

| [Btn] [Btn] [#####et#####] |

मैंने कई अलग-अलग संयोजनों की कोशिश की है जो मैं उन्हें याद भी नहीं कर सकता, और किसी ने भी काम नहीं किया।

जवाबों:


213

यह काम नहीं करता क्योंकि आप fill_parent को चौड़ाई के रूप में उपयोग कर रहे हैं। वजन का उपयोग शेष खाली स्थान को वितरित करने या अंतरिक्ष को दूर करने के लिए किया जाता है जब कुल राशि लिनियरलेयूट से बड़ी होती है। इसके बजाय अपनी चौड़ाई 0dip पर सेट करें और यह काम करेगा।


6
0dip के लिए रैखिक लेआउट पर चौड़ाई सेट करना जैसे मैंने चाहा वैसे ही काम किया। मुझे नहीं पता था कि यह एक विकल्प था। यह समझ में आता है कि ऐसा क्यों काम करेगा जहां रैप_ कॉन्टेंट नहीं होगा। जब कोई सामग्री न हो तो उसे लपेटने के लिए चौड़ाई की चौड़ाई तय करना। लेकिन इसे 0dip पर सेट करने से इसे कुछ भी नहीं फैलने की अनुमति मिलती है, हालांकि इसके लिए बहुत बड़ी आवश्यकता होती है। आपका बहुत बहुत धन्यवाद।
अनलकीडेविल

"Fill_parent" और layout_weight को "0.2", "0.85" आदि जैसे फ़्लोट मान पर लेआउट_ एक्सपोज़र सेट करना भी काम करता है। इस मामले में जितनी बड़ी संख्या में छोटे आकार का दृश्य होगा। क्या आप यह समझा सकते हैं कि क्या यह करने का एक स्वीकार्य तरीका है?
यार

37
यह मेरे लिए भी काम किया। धन्यवाद। (हालांकि मुझे कहना है, क्या किसी और को एंड्रॉइड लेआउट सिस्टम अजीब और अनपेक्षित लगता है?)
tstyle

धन्यवाद! मुझे वास्तव में यह बताने में समस्या थी कि मेरी लेआउट चौड़ाई उम्मीद के मुताबिक क्यों नहीं दिखाई दी। यह 0dip सेटिंग थी जिसने यह किया :)
20:11

अगर आप आसान काम करना चाहते हैं तो @tstyle android आसान है। हालाँकि यदि आप आसान चीजों से अधिक करना चाहते हैं, तो यह कठिन हो जाता है।
पचेरियर

14

android:Layout_weightका उपयोग तब किया जा सकता है जब आप अपनी चौड़ाई के लिए पहले से ही जैसे एक ठीक मूल्य संलग्न न करें fill_parentआदि।

कुछ इस तरह से करें:

<Button>

Android:layout_width="0dp"
Android:layout_weight=1

-----other parameters
</Button>

7

इसे इस तरह से सोचें, सरल हो जाएगा

यदि आपके पास 3 बटन हैं और उनके वजन के अनुसार 1,3,1 हैं, तो यह HTML में तालिका की तरह काम करेगा

उस लाइन के लिए 5 भाग प्रदान करें: बटन 1 के लिए 1 भाग, बटन 2 के लिए 3 भाग और बटन 1 के लिए 1 भाग


3

लीनियर लयआउट में वेटसम = 2 सेट करें;

और अपने बच्चों को वजन वितरित करें जैसा कि आप उन्हें प्रदर्शित करना चाहते हैं .. मैंने बच्चे को वजन = "1" दिया है। इसलिए दोनों कुल का आधा वितरित करेंगे।

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

    <ImageView
        android:id="@+id/ring_oss"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ring_oss" />

    <ImageView
        android:id="@+id/maila_oss"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/maila_oss" />
</LinearLayout>

2

वजन मान, उपलब्ध मूल्य के 0-1 शेयर वितरण (लेआउट_ एक्सपोज़र = "0px" सेट करने के बाद) वजन मूल्य के अनुपात में। निर्दिष्ट नहीं (बिना वेट एंट्री) वजन वाले तत्वों को वजन 0 मिलता है जिसका अर्थ है कि उन्हें कोई विस्तार नहीं मिलता है।

बिना वजन वाली प्रविष्टियों के लिए एक सरल विकल्प, मार्की को पाठ के साथ देखने के लिए संलग्न करना है, जो इसे पाठ के लिए न्यूनतम आवश्यक से विस्तार करने के लिए कहता है (wra_content) उपलब्ध स्थान को संपादित करें।


0

मुझे लगता है कि EditTexts की चौड़ाई को सेट करने के लिए wrap_contentऔर दो बटन LinearLayoutजिनकी चौड़ाई fill_parent1 है और वजन 1 पर सेट किया जाएगा।


0

एक और कारण मुझे मिला (अस्पष्ट के रूप में यह लग सकता है)। नीचे काम नहीं किया।

रेखीय लयआउट वर्टिकल

रेखीय लयआउट ऊंचाई भराव + वजन

रेखीय लयआउट ऊंचाई भराव + वजन

रेखीय लयआउट ऊंचाई भराव + वजन

EndLinearLayout

क्या काम था

अंतरंग परिस्थिति

रेखीय लयआउट वर्टिकल

रेखीय लयआउट ऊंचाई भराव + वजन

रेखीय लयआउट ऊंचाई भराव + वजन

रेखीय लयआउट ऊंचाई भराव + वजन

EndLinearLayout

EndRelativeLayout

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


0
<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="9"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/ring_oss"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/ring_oss" />

    <ImageView
        android:id="@+id/maila_oss"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/maila_oss" />
<EditText
        android:id="@+id/edittxt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:src="@drawable/maila_oss" />
</LinearLayout>
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.