Android-Fragmente: Fragmente innerhalb eines Rahmens oder andere layouts?
Erstelle ich eine Android app jetzt und ich habe einen Rahmen, der Schale enthalten Fragmente. Bis jetzt alles Prima funktioniert, aber ich habe eine Frage, die ich nicht ganz verstehen.
Ich habe ein Frame, welches den gesamten Bildschirm (FrameLayout - sollte ich ein FrameLayout als der main-Frame?) und innerhalb dieses Rahmens gibt es Fragmente, die sich ändern, abhängig von der Benutzer-Interaktion. Diese Fragmente sind in der .xml-Dateien FrameLayouts. Ich Frage mich jetzt, ob Sie können oder sollten FrameLayouts oder Fragmente... erstellt habe ich eine Google Maps frgment das ist ein fragment ideed und das machte mich zu denken.
Also meine Frage ist: Macht es einen Unterschied, oder hat es irgendwelche Auswirkungen auf die Performance oder kann ich das einfach verwenden, was auch immer dient es den Zweck?
Zeigen was ich meine, hier sind einige code-Beispiele:
(Im FrameLayout im inneren des Framelayout sind alle Fragmente setzen)
MainFrame - (activity_main.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id = "@+id/mainFrame"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
</FrameLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "@dimen/bottom_Main_Tabs"
android:layout_gravity = "bottom"
android:background="@color/grey2"
>
<ImageButton
android:id = "@+id/bottomButton_home"
android:layout_height = "match_parent"
android:layout_width = "0dp"
android:layout_weight = "1.0"
android:layout_marginLeft = "2dp"
android:layout_marginRight = "2dp"
android:background = "@drawable/ic_home_white"
android:onClick = "openHome"
/>
[...]
</LinearLayout>
<RelativeLayout
android:id = "@+id/TopBar"
android:layout_width = "match_parent"
android:layout_height = "@dimen/top_Bar_Hight"
android:layout_gravity="top"
android:background="@color/grey_transparentBy50"
>
<ImageView
android:id= "@+id/TopBarLogo"
android:layout_width = "@dimen/top_Bar_Hight"
android:layout_height = "@dimen/top_Bar_Hight"
android:background="@drawable/ic_launcher"
/>
[...]
</RelativeLayout>
</FrameLayout>
Einem Fragment (FrameLayout:)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context="com.domain.app.HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:textSize="100sp"
android:text="Home" />
</FrameLayout>
Anderen Frament (fragment)
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.domain.app.MapsActivity"
android:name="com.google.android.gms.maps.MapFragment"
tools:layout = "@layout/activity_main"
/>
Ist zu hae Probleme mit der GoogleMaps-Implementierung, aber ich habe nicht genug Zeit auf es noch, zu Fragen, hier helfen.
Vielen Dank im Voraus.
John
- könnten Sie fügen Sie xml-Dateien Inhalte?
- Frage ist aktualisiert
Du musst angemeldet sein, um einen Kommentar abzugeben.
Wenn Sie anbringen, Fragmente in xml, wie GoogleMapFragment oder durch die Verwendung von FrameLayout(oder einer anderen ViewGroup wie LinearLayout oder RelativeLayout) in xml. Beide Wege sind gleich. Es ist eher die Schaffung einer textview programmgesteuert (z.B. in Java) oder der Definition in xml.
Verwendung fragment in Java:
und in xml -
Hier R. id.fragment_container ist die id des Frame-Layout.
Verwendung fragment in XML:
Im Fall von Fragment in Ihre XML-fragment wird definiert durch
<fragment/>
xml-tag und auf der anderen Seite, wenn wir mit Fragmenten programmgesteuert, Ihr FrameLayout funktioniert als container für Ihre fragment.Fazit: Mit FrameLayout erhöhen eine Hierarchie, aber es wird keinen Einfluss auf Ihre Leistung sehr.
Finden Sie unter: http://developer.android.com/guide/components/fragments.html#Adding