Wie fügt man Layout in Layout in Android ein?
Ich erstelle ein gemeinsames Layout. Ich möchte dieses Layout in eine andere Seite einfügen.
mohan
Wie fügt man Layout in Layout in Android ein?
Ich erstelle ein gemeinsames Layout. Ich möchte dieses Layout in eine andere Seite einfügen.
Michael Rosa
Bearbeiten: Wie in einem Kommentar zu Recht erbeten hier noch ein paar Infos. Verwenden Sie die include
Schild
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/yourlayout" />
um das Layout einzuschließen, das Sie wiederverwenden möchten.
Überprüfen dieser Link aus…
Nur ein kleines Detail: Verwenden Sie android:layout_width=”match_parent” anstelle von android:layout_width=”fill_parent”. fill_parent ist veraltet.
– Agna JirKon Rx
24. Mai 2017 um 14:33 Uhr
Kann ich ein Layout einbinden und einige seiner Eigenschaften über die XML-Datei festlegen, z. B. eine Textzeichenfolge im Sublayout direkt im
– JohnyTex
3. Mai 2019 um 10:22 Uhr
@JohnyTex Nicht sicher, ob Sie es direkt im tun können <include />
-Tag, Sie können dies jedoch mit Java-Code tun. Sehen Sie sich die Antwort von Phileo99 unten an, um zu erfahren, wie Sie einen Verweis auf das enthaltene Layout erhalten. und dann können Sie den Inhalt ändern.
– Musa Halaseh
5. Oktober 2019 um 16:44 Uhr
Beachten Sie, dass es KEINE NOTWENDIGKEIT gibt, die Layout-Elemente in das include-Tag aufzunehmen. Sie können sie einfach im “enthaltenen Körper” (der Unterdatei) haben und es funktioniert genauso perfekt.
– Fett
9. März 2021 um 11:47 Uhr
Beachten Sie, dass, wenn Sie einbeziehen android:id...
in die <include />
-Tag, überschreibt es die ID, die im enthaltenen Layout definiert wurde. Zum Beispiel:
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/some_id_if_needed"
layout="@layout/yourlayout" />
ihrlayout.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>
Dann würden Sie dieses enthaltene Layout im Code wie folgt referenzieren:
View includedLayout = findViewById(R.id.some_id_if_needed);
Button insideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button1);
GrAnd
Verwenden <include />
Schild.
<include
android:id="@+id/some_id_if_needed"
layout="@layout/some_layout"/>
Lesen Sie auch Wiederverwendbare UI-Komponenten erstellen und Zusammenführen von Layouts Artikel.
Beide Links existieren nicht mehr.
– Jimmy
16. April 2014 um 14:26 Uhr
Versuche dies
<include
android:id="@+id/OnlineOffline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="@layout/YourLayoutName" />
Aus offiziellen Dokumenten über Layouts wiederverwenden
Obwohl Android eine Vielzahl von Widgets bietet, um kleine und wiederverwendbare interaktive Elemente bereitzustellen, müssen Sie möglicherweise auch größere Komponenten wiederverwenden, die ein spezielles Layout erfordern. Um vollständige Layouts effizient wiederzuverwenden, können Sie das Tag verwenden, um ein anderes Layout in das aktuelle Layout einzubetten.
Hier ist mein header.xml Datei, die ich wiederverwenden kann Tag enthalten
<?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>
Nein ich benutze die -Tag in XML, um ein weiteres Layout aus einer anderen XML-Datei hinzuzufügen.
<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>
Warum die TextView in ein RelativeLayout setzen und nicht als Root-View?
– Florian Walther
27. März 2018 um 12:36 Uhr
@FlorianWalther Dies ist ein Beispiel
– IntelliJ Amiya
27. März 2018 um 12:46 Uhr
Danke für die schnelle Antwort. Aber habe ich Recht, dass ich die TextView als Root-Element verwenden könnte, oder übersehe ich etwas? Weil ich eine ProgressBar wiederverwenden möchte und mich frage, ob ich sie in ein Layout einfügen muss.
– Florian Walther
27. März 2018 um 12:48 Uhr
@FlorianWalther Because I want to reuse a ProgressBar
welches Problem kommt?
– IntelliJ Amiya
27. März 2018 um 12:52 Uhr
Es gibt kein Problem, es funktioniert. Aber alle Beispiele, die ich online sehe, setzen das einzelne Widget in ein anderes Layout und ich frage mich, warum.
– Florian Walther
27. März 2018 um 12:53 Uhr
Erfahren Sie mehr über diesen Link
https://developer.android.com/training/improving-layouts/reusing-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>
Blockzitat
Das obige Layout können Sie in anderen Aktivitäten verwenden
<?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>
Warum die TextView in ein RelativeLayout setzen und nicht als Root-View?
– Florian Walther
27. März 2018 um 12:36 Uhr
@FlorianWalther Dies ist ein Beispiel
– IntelliJ Amiya
27. März 2018 um 12:46 Uhr
Danke für die schnelle Antwort. Aber habe ich Recht, dass ich die TextView als Root-Element verwenden könnte, oder übersehe ich etwas? Weil ich eine ProgressBar wiederverwenden möchte und mich frage, ob ich sie in ein Layout einfügen muss.
– Florian Walther
27. März 2018 um 12:48 Uhr
@FlorianWalther Because I want to reuse a ProgressBar
welches Problem kommt?
– IntelliJ Amiya
27. März 2018 um 12:52 Uhr
Es gibt kein Problem, es funktioniert. Aber alle Beispiele, die ich online sehe, setzen das einzelne Widget in ein anderes Layout und ich frage mich, warum.
– Florian Walther
27. März 2018 um 12:53 Uhr
Es gibt ein einfaches Anwendungsbeispiel [in this post][1] [1]: stackoverflow.com/questions/2732682/…
– Asaf Pinhassi
11. August 2011 um 8:20 Uhr