लेआउट के अंदर लेआउट कैसे शामिल करें?


92

एंड्रॉइड में लेआउट के अंदर लेआउट कैसे शामिल करें?

मैं कॉमन लेआउट बना रहा हूं। मैं उस लेआउट को दूसरे पेज में शामिल करना चाहता हूं।


एक सरल उपयोग उदाहरण है [इस पोस्ट में] [1] [१]: stackoverflow.com/questions/2732682/…
Asaf Pinhassi

जवाबों:


193

संपादित करें: जैसा कि एक टिप्पणी में यहाँ कुछ और जानकारी का अनुरोध किया गया है। includeटैग का उपयोग करें

<include
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   layout="@layout/yourlayout" />

उस लेआउट को शामिल करना जिसे आप पुन: उपयोग करना चाहते हैं।

चेक इस लिंक बाहर ...


11
बस एक छोटा सा विवरण: एंड्रॉइड के बजाय एंड्रॉइड: लेआउट_प्रकार = "मैच_परेंट" का उपयोग करें: लेआउट_प्रकार = "फ़िल_परेंट"। fill_parent निकाला जाता है।
ट्रिनिटी

1
क्या मैं एक लेआउट को शामिल कर सकता हूं और इसके कुछ गुणों को xml के माध्यम से सेट कर सकता हूं, उदाहरण के लिए <उपबंध में सीधे एक टेक्स्ट स्ट्रिंग सेट करें <?
जॉनीटेक्स

@JohnyTex यकीन नहीं है कि अगर आप इसे सीधे <include />टैग में कर सकते हैं, हालांकि, आप इसे जावा कोड का उपयोग करके कर सकते हैं। देखने के लिए नीचे दिए गए Phileo99 का जवाब कैसे शामिल लेआउट के लिए एक संदर्भ पाने के लिए पता करने के लिए। और फिर आप इसे सामग्री में बदल सकते हैं।
मूसा

56

ध्यान दें कि यदि आप टैग android:id...में शामिल करते हैं <include />, तो इसमें शामिल लेआउट के अंदर जो भी आईडी परिभाषित की गई थी, वह ओवरराइड हो जाएगी। उदाहरण के लिए:

<include
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_id_if_needed"
   layout="@layout/yourlayout" />

yourlayout.xml:

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/some_other_id">
   <Button
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/button1" />
 </LinearLayout>

फिर आप इस कोड में शामिल लेआउट को निम्नानुसार संदर्भित करेंगे:

View includedLayout = findViewById(R.id.some_id_if_needed);
Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);


6

इसे इस्तेमाल करे

<include
            android:id="@+id/OnlineOffline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            layout="@layout/YourLayoutName" />

2

लेआउट्स के पुन: उपयोग के बारे में आधिकारिक दस्तावेजों से

यद्यपि एंड्रॉइड छोटे और पुन: उपयोग करने योग्य इंटरैक्टिव तत्वों को प्रदान करने के लिए विभिन्न प्रकार के विजेट प्रदान करता है, आपको बड़े घटकों को फिर से उपयोग करने की आवश्यकता हो सकती है जिनके लिए एक विशेष लेआउट की आवश्यकता होती है। पूर्ण लेआउट का कुशलता से पुन: उपयोग करने के लिए, आप वर्तमान लेआउट के अंदर किसी अन्य लेआउट को एम्बेड करने के लिए टैग का उपयोग कर सकते हैं।

यहाँ मेरा हैडर। Xml फ़ाइल है जिसका मैं टैग का उपयोग करके पुन: उपयोग कर सकता हूं

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/app_name"
        android:textColor="#000000" />

</RelativeLayout>

नहीं मैं उपयोग नहीं करता XML में टैग किसी अन्य XML फ़ाइल से एक और लेआउट जोड़ने के लिए।

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


    <include
        android:id="@+id/header_VIEW"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/header" />

        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#ffffff"
        android:orientation="vertical"
        android:padding="5dp" >


    </LinearLayout>

TextView को एक RelativeLayout में क्यों रखा जाए और रूट दृश्य के रूप में नहीं?
फ्लोरियन वाल्थर

@FlorianWalther यह एक उदाहरण है
IntelliJ Amiya

शीघ्र जवाब देने के लिए ध्न्यवाद। लेकिन क्या मैं सही हूं कि मैं TextView को मूल तत्व के रूप में रख सकता हूं या क्या मुझे कुछ याद आ रहा है? क्योंकि मैं एक ProgressBar का पुन: उपयोग करना चाहता हूं और मुझे आश्चर्य होता है कि क्या मुझे इसे एक लेआउट में रखना है।
फ्लोरियन वाल्थर

@FlorianWalther Because I want to reuse a ProgressBarक्या समस्या आ रही है?
इंटेलीज अमिया

कोई समस्या नहीं है, यह काम करता है। लेकिन मेरे द्वारा देखे गए सभी उदाहरण एकल विजेट को किसी अन्य लेआउट में डालते हैं और मुझे आश्चर्य होता है कि क्यों।
फ्लोरियन वाल्थर

0

इस लिंक का उपयोग करके अधिक जानें https://developer.android.com/training/improving-layouts/reuse-layouts.html

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Game_logic">
    
          
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:id="@+id/text1"
                    android:textStyle="bold"
                    tools:text="Player " />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:layout_marginLeft="20dp"
    
                    android:id="@+id/text2"
                    tools:text="Player 2" />
          
            
          
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

Blockquote

  • लेआउट के ऊपर आप अन्य गतिविधि का उपयोग कर सकते हैं

     <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                      xmlns:app="http://schemas.android.com/apk/res-auto"
                      xmlns:tools="http://schemas.android.com/tools"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      tools:context=".SinglePlayer">   
    
              <include layout="@layout/activity_game_logic"/> 
          </androidx.constraintlayout.widget.ConstraintLayout>
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.