व्यू के साथ लेआउट बनाने के लिए शेष स्थान कैसे भरें?


188

मैं अपना एप्लिकेशन UI डिजाइन कर रहा हूं। मुझे इस तरह एक लेआउट की आवश्यकता है:

वांछित लेआउट का उदाहरण

(<और> बटन हैं)। समस्या यह है, मुझे नहीं पता कि यह सुनिश्चित करने के लिए कि TextView शेष स्थान को कैसे भरेगा, दो बटन के साथ निश्चित आकार होगा।

यदि मैं टेक्स्ट दृश्य के लिए fill_parent का उपयोग करता हूं, तो दूसरा बटन (>) नहीं दिखाया जा सकता है।

मैं एक लेआउट को कैसे चित्रित कर सकता हूं जो छवि की तरह दिखता है?


आप रूट लेआउट के रूप में क्या उपयोग करते हैं?
वन्ध्या

1
@woodshy कोई भी लेआउट ठीक है, इससे कोई फर्क नहीं पड़ता।
ल्यूक वो

जवाबों:


211

वुडी से जवाब मेरे लिए काम किया, और यह Ungureanu Liviu द्वारा जवाब से सरल है क्योंकि यह उपयोग नहीं करता है RelativeLayout। मैं अपना लेआउट स्पष्टता के लिए दे रहा हूं:

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

     <Button
        android:layout_width = "80dp"
        android:layout_weight = "0"
        android:layout_height = "wrap_content"
        android:text="&lt;"/>
     <TextView
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"/>
     <Button
        android:layout_width = "80dp"
        android:layout_weight = "0"
        android:layout_height = "wrap_content"
        android:text="&gt;"/>   
 </LinearLayout>

कुछ alignLeftऔर alignParentLeft, आदि हैं, जो कभी भी प्राप्त नहीं किए जा सकते हैं LinearLayout
इचीफ्लेम

मुझे पूरी तरह से समझ में नहीं आया कि यह कैसे संभव है लेकिन RelativeLayout से बचने के लिए यह वास्तव में बहुत अच्छी चाल है
a.dibacco

2
यह स्वीकृत उत्तर कैसे हो सकता है? कोई छवि दृश्य नहीं है, यह केवल समाधान का हिस्सा है। रिलेटिव लेआउट के साथ पुराने टॉप वोटेड उत्तर की तुलना में कम से कम यह बेहतर है (मैंने अपने उत्तर में क्यों समझाया)
लिवियो

जीनियस समाधान
मोहम्मद एल्सेयेद

92

यदि <TEXT VIEW> को LinearLayout में रखा गया हो, तो TextView के लिए <और> से 0 और 1 का लेआउट_वेट प्रॉपर सेट करें।
RelativeLayout के मामले में <और> बाएं और दाएं संरेखित करें और "लेआउट के बाईं ओर" और "लेआउट से दाईं ओर" टेक्स्ट व्यू की संपत्ति को <और> के लेआउट में सेट करें।


क्या किसी को अभी भी नमूना की आवश्यकता है?
वन्ध्या

पूरे दृश्य को भरने के लिए इस लेआउट में छवियों को कैसे जोड़ा जाए
तुषार पांडे

आप पूरे दृश्य को भरने के लिए रिश्तेदार लेआउट के अंदर छवि को कैसे जोड़ेंगे
तुषार पांडे

4
@वुड्शी हां एक नमूना अभी भी मददगार होगा
फ्रैंक श्वाइटमैन


71

यदि आप उपयोग करते हैं RelativeLayout, तो आप इसे कुछ इस तरह से कर सकते हैं:

<RelativeLayout
    android:layout_width = "fill_parent"
    android:layout_height = "fill_parent">
    <ImageView
        android:id = "@+id/my_image"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_alignParentTop ="true" />
    <RelativeLayout
        android:id="@+id/layout_bottom"
        android:layout_width="fill_parent"
        android:layout_height = "50dp"
        android:layout_alignParentBottom = "true">
        <Button
            android:id = "@+id/but_left"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="&lt;"
            android:layout_alignParentLeft = "true"/>
        <TextView
            android:layout_width = "fill_parent"
            android:layout_height = "wrap_content"
            android:layout_toLeftOf = "@+id/but_right"
            android:layout_toRightOf = "@id/but_left" />
        <Button
            android:id = "@id/but_right"
            android:layout_width = "80dp"
            android:layout_height = "wrap_content"
            android:text="&gt;"
            android:layout_alignParentRight = "true"/>
    </RelativeLayout>
</RelativeLayout>

धन्यवाद, यह काम करता है :) लेकिन मुझे समझ नहीं आता। TextView सभी स्थान क्यों नहीं भरता, >बटन के लिए नहीं ?
ल्यूक वो

2
दो नेस्टेड रिश्तेदार लेआउट क्यों हैं? रूट एक पर्याप्त होना चाहिए। फिर, आपको बस यह सुनिश्चित करने की आवश्यकता है कि नीचे के रिश्तेदार लेआउट के बच्चे सभी संरेखित करें Bottom = "सत्य"
Noel

@ कोई बात नहीं आप सही हैं। मैं 2 लेआउट चुनता हूं क्योंकि यह वह तरीका है जिसका मैं उपयोग कर रहा हूं: मेरे अनुप्रयोगों में ऐसा हो सकता है कि मुझे कुछ दृश्यों की दृश्यता बदलने की आवश्यकता है और उन सभी को एक लेआउट में रखना आसान है और इस लेआउट के लिए दृश्यता को बदलना । मेरा उदाहरण सही नहीं है, लेकिन यह काम करता है और यह किसी के लिए उपयोगी हो सकता है।
उंगुरेनु लिवियू

1
@ जब यह मेरे लिए भी रूखा हो जाता है :) मुझे लगता है कि टेक्स्टव्यू सभी जगह का उपयोग नहीं करते क्योंकि दाहिने बटन की एक निश्चित चौड़ाई होती है। यदि आप Android बदलते हैं: लेआउट_ एक्सपोज़र = "80dp" से एंड्रॉइड: लेआउट_ एक्सपोज़र = राइट-बटन के लिए "रैप_ कॉन्टेंट" यह काम करता है।
अनगरुएनु लिविउ

2
यह उत्तर वास्तव में बुरा है! आपको 2 रिश्तेदार लेआउट के घोंसले से बचना चाहिए क्योंकि सापेक्ष लेआउट हमेशा ड्राइंग के लिए 2 पास बनाते हैं (किसी अन्य प्रकार के लेआउट के लिए 1)। जब आप उन्हें घोंसला देते हैं तो यह घातीय हो जाता है। आपको उस तत्व पर चौड़ाई = 0 और भार = 1 के साथ रेखीय लेआउट का उपयोग करना चाहिए जिसे आप बचे हुए स्थान को भरना चाहते हैं। याद रखें: जब आपके पास कोई अन्य विकल्प न हो तो केवल सापेक्ष लेआउट का उपयोग करें। stackoverflow.com/questions/4069037/…
Livio

30

A का उपयोग करते हुए ConstraintLayout, मुझे कुछ ऐसा मिला है

<Button
    android:id="@+id/left_button"
    android:layout_width="80dp"
    android:layout_height="48dp"
    android:text="&lt;"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/left_button"
    app:layout_constraintRight_toLeftOf="@+id/right_button"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/right_button"
    android:layout_width="80dp"
    android:layout_height="48dp"
    android:text="&gt;"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

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


4
बस कांस्ट्रैटिनलाईट का उपयोग करना शुरू करना, यह जानना बहुत अच्छा है कि "0" पर सेट करते समय चौड़ाई और ऊंचाई का निर्धारण बाधाओं से किया जा सकता है, इसके लिए
थैंक्स

प्रत्येक बाधा के तीर की दिशा के बारे में सावधान रहना महत्वपूर्ण है । सुनिश्चित करें कि TextViewबाएँ और दाएँ अवरोध हैं। TextViewखिंचाव नहीं करता है तो पहले Buttonका अधिकार बाधा है TextViewऔर पिछले Buttonकरने के लिए छोड़ दिया बाधा है TextView
मैनुएल

काफी बेहतर! नेस्टेड लेआउट नहीं होने से प्रदर्शन में सुधार। 0dp चाल की याद दिलाने के लिए शुक्रिया
OzzyTheGiant 4:29 बजे

7

यह सरल है आप मिनीवेट या मिनीहाइट सेट करते हैं, इस पर निर्भर करता है कि आप क्या देख रहे हैं, क्षैतिज या ऊर्ध्वाधर। और दूसरी वस्तु के लिए (जो आप शेष स्थान को भरना चाहते हैं) आप 1 का वजन सेट करते हैं (यह सामग्री को लपेटने के लिए चौड़ाई सेट करें), इसलिए यह शेष क्षेत्र को भर देगा।

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center|left"
        android:orientation="vertical" >
</LinearLayout>
<LinearLayout
        android:layout_width="80dp"
        android:layout_height="fill_parent"
        android:minWidth="80dp" >
</LinearLayout>

4

आप उच्च लेआउट_वेट विशेषता का उपयोग कर सकते हैं। नीचे आप एक लेआउट देख सकते हैं जहाँ ListView नीचे बटन के साथ सभी खाली जगह लेता है:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".ConfigurationActivity"
    android:orientation="vertical"
    >

        <ListView
            android:id="@+id/listView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1000"
            />


        <Button
            android:id="@+id/btnCreateNewRule"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Create New Rule" />



        <Button
            android:id="@+id/btnConfigureOk"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Ok" />


</LinearLayout>

3

<LinearLayout...>जैसा मैंने किया था , वैसा ही गड़बड़ करने वालों के लिए :

यह निर्दिष्ट करना महत्वपूर्ण है android:layout_width="fill_parent", यह साथ काम नहीं करेगा wrap_content

OTOH, आप छोड़ सकते हैं android:layout_weight = "0", यह आवश्यक नहीं है।

मेरा कोड मूल रूप से https://stackoverflow.com/a/25781167/755804 (विवेक पांडे द्वारा) कोड के समान है


3

आपको 2 रिश्तेदार लेआउट के घोंसले से बचना चाहिए क्योंकि रिश्तेदार लेआउट हमेशा ड्राइंग के लिए 2 पास बनाते हैं (किसी अन्य प्रकार के लेआउट के लिए 1)। जब आप उन्हें घोंसला देते हैं तो यह घातीय हो जाता है। आपको उस तत्व पर चौड़ाई = 0 और भार = 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="match_parent"
          android:orientation="vertical">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/prev_button"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="&lt;" />

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

        <Button
            android:id="@+id/next_button"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:text="&gt;" />
    </LinearLayout>
</LinearLayout>

0

आप सेट का उपयोग कर सकते हैं layout_widthया layout_widthकरने के लिए 0dp(उन्मुखीकरण करके आप शेष स्थान को भरने के लिए चाहते हैं)। फिर layout_weightइसे शेष स्थान को भरने के लिए उपयोग करें ।


0

LinearLayout को लपेटने के लिए किसी Relativelayout का उपयोग करें

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:round="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
    <Button
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text="&lt;"/>
    <TextView
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"/>
    <Button
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text="&gt;"/>

</LinearLayout>

</RelativeLayout>`

0

मुझे मिला

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="10dp"
        android:fontFamily="casual"
        android:text="(By Zeus B0t)"
     ``   android:textSize="10sp"
        android:gravity="bottom"
        android:textStyle="italic" />

0

जब एक रिश्तेदार लेआउट का उपयोग करते हैं, तो आप इसे उन दोनों दृश्यों के साथ जोड़कर एक दृश्य खिंचाव बना सकते हैं, जिन्हें खिंचाव की ओर माना जाता है। यद्यपि निर्दिष्ट ऊंचाई की अवहेलना की जाएगी, फिर भी एंड्रॉइड को एक ऊंचाई विशेषता की आवश्यकता है, यही वजह है कि मैंने "0dp" लिखा। उदाहरण:

<View
    android:id="@+id/topView"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="8dp"/>

<View
    android:id="@+id/stretchableView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@id/topView"
    android:layout_above="@+id/bottomView"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:adjustViewBounds="true"/>

<View
    android:id="@id/bottomView"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="16dp"/>
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.