एंड्रॉइड में लेआउट के अंदर लेआउट कैसे शामिल करें?
मैं कॉमन लेआउट बना रहा हूं। मैं उस लेआउट को दूसरे पेज में शामिल करना चाहता हूं।
जवाबों:
संपादित करें: जैसा कि एक टिप्पणी में यहाँ कुछ और जानकारी का अनुरोध किया गया है। includeटैग का उपयोग करें
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/yourlayout" />
उस लेआउट को शामिल करना जिसे आप पुन: उपयोग करना चाहते हैं।
चेक इस लिंक बाहर ...
<include />टैग में कर सकते हैं, हालांकि, आप इसे जावा कोड का उपयोग करके कर सकते हैं। देखने के लिए नीचे दिए गए Phileo99 का जवाब कैसे शामिल लेआउट के लिए एक संदर्भ पाने के लिए पता करने के लिए। और फिर आप इसे सामग्री में बदल सकते हैं।
ध्यान दें कि यदि आप टैग 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);
<include />टैग का उपयोग करें ।
<include
android:id="@+id/some_id_if_needed"
layout="@layout/some_layout"/>
इसके अलावा, पुन: प्रयोज्य UI घटक और विलय लेआउट लेख पढ़ना ।
लेआउट्स के पुन: उपयोग के बारे में आधिकारिक दस्तावेजों से
यद्यपि एंड्रॉइड छोटे और पुन: उपयोग करने योग्य इंटरैक्टिव तत्वों को प्रदान करने के लिए विभिन्न प्रकार के विजेट प्रदान करता है, आपको बड़े घटकों को फिर से उपयोग करने की आवश्यकता हो सकती है जिनके लिए एक विशेष लेआउट की आवश्यकता होती है। पूर्ण लेआउट का कुशलता से पुन: उपयोग करने के लिए, आप वर्तमान लेआउट के अंदर किसी अन्य लेआउट को एम्बेड करने के लिए टैग का उपयोग कर सकते हैं।
यहाँ मेरा हैडर। 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>
Because I want to reuse a ProgressBarक्या समस्या आ रही है?
इस लिंक का उपयोग करके अधिक जानें 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>