2016-07-19 4 views
0

Klingt wie eine Frage, die vorher beantwortet wurde, aber von meiner Forschung konnte ich keine Lösung finden. Mein Layout ist in etwa die folgenden:Wie man eine Ansicht auf meine Fragmente behält

enter image description here

Der Hauptbereich ein Behälter ist, wo ich werde Fragmente hinzufügen/ersetzen, und der Boden ist, wo ich eine untere Navigationsmenü platzieren. Die rosa Ansicht ist eine Schaltfläche, die ich oben auf meinem Menü platzieren muss (und das funktioniert nicht).

Jedes Mal, wenn ich ein Fragment zur Hauptansicht hinzufüge, verschwindet mein "Menü" -Text, obwohl TextView nach der Containeransicht hinzugefügt wird. Gleiches gilt für die pinkfarbene Schaltfläche, die nach dem unteren Menü-Container hinzugefügt wird (siehe mein Xml unten).

Wie kann ich sowohl "Menu" TextView als auch den pinkfarbenen Button immer über meinen Fragmenten halten?

Das ist mein xml:

<RelativeLayout 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:background="#eee" 
    tools:context="com.wecancer.wecancer.activities.Menu"> 

    <FrameLayout 
     android:id="@+id/main_container_view" 
     android:layout_width="match_parent" 
     android:layout_height="520dp" 
     android:layout_marginBottom="0dp" 
     > 
    </FrameLayout> 

    <TextView 
     android:id="@+id/main_center_tv" 
     android:layout_centerVertical="true" 
     android:textSize="24sp" 
     android:layout_centerHorizontal="true" 
     android:text="@string/menu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <FrameLayout 
     android:layout_alignParentBottom="true" 
     android:id="@+id/main_bottom_menu_container" 
     android:layout_width="match_parent" 
     android:background="@color/lightGray" 
     android:layout_height="40dp" 
     > 

    </FrameLayout> 

    <Button 
     android:elevation="1dp" 
     android:id="@+id/menu_plus_img" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentBottom="true" 
     android:background="@color/colorAccent" 
     android:layout_width="70dp" 
     android:layout_marginBottom="0dp" 
     android:layout_height="70dp" 
     tools:targetApi="lollipop" /> 

</RelativeLayout> 

Antwort

0

Sie müssen nur die Reihenfolge der Ansichten im Layout ändern, während ein Framelayout

Die Komplexität des Layouts eines RelativeLayout erfordert keine Verwendung, so dass Sie nur muss wechseln.

+0

Geben Sie bitte eine praktische Lösung. Das ist nicht wirklich eine Antwort, da ich sagte, dass die Bestellung korrekt sein sollte und Sie sagen "ändern Sie einfach die Reihenfolge". –

1

Versuchen This`

<FrameLayout 
    android:layout_alignParentBottom="true" 
    android:id="@+id/main_bottom_menu_container" 
    android:layout_width="match_parent" 
    android:background="@color/colorPrimary" 
    android:layout_height="40dp" 
    > 

</FrameLayout> 

<Button 
    android:elevation="1dp" 
    android:id="@+id/menu_plus_img" 
    android:background="@color/colorAccent" 
    android:layout_width="70dp" 
    android:layout_height="70dp" 
    tools:targetApi="lollipop" 
    android:layout_marginLeft="71dp" 
    android:layout_marginStart="71dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:id="@+id/main_center_tv" 
    android:textSize="24sp" 
    android:text="@string/menu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="16dp" 
    android:layout_marginStart="16dp" 
    android:layout_alignBaseline="@+id/menu_plus_img" 
    android:layout_alignBottom="@+id/menu_plus_img" 
    android:layout_toRightOf="@+id/menu_plus_img" 
    android:layout_toEndOf="@+id/menu_plus_img" /> 

<FrameLayout 
    android:id="@+id/main_container_view" 
    android:layout_width="match_parent" 
    android:layout_height="520dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/menu_plus_img"> 


</FrameLayout> 

`

+0

Es funktionierte für die TextView aber nicht für die Schaltfläche, aber warum? Können Sie eine Erklärung hinzufügen, warum der TextView jetzt oben wäre? –

+0

Sie verwendet layout_centerHorizontal = "True" in der Textansicht, die es in der Mitte des Bildschirms und oben auf das Rahmenlayout legen, das beim Start leer ist, aber wenn Sie Fragment laden, wird die darunter liegende Textansicht ausgeblendet. Welches Problem haben Sie mit dem Knopf? – Nofi

+0

Ich benutze diese Bibliothek https://github.com/roughike/BottomBar, die anscheinend Elemente programmatisch hinzufügt. Daher kann ich meinen Button nicht nach vorne bringen, selbst wenn bringToFront() verwendet wird, nachdem die Ansicht erstellt wurde. –

0

Ich weiß nicht wirklich, warum Methoden wie bringToFront, bringChildToFront oder das Hinzufügen und Entfernen von Ansicht Kinder ihren Indizes zu ändern, hat nicht funktioniert. Ich musste also eine neue Layoutstruktur erstellen.

Statt:

RelativeLayout 
...FrameLayout 
...TextView 
...FrameLayout 
...Button 

ich getan habe:

FrameLayout 
...RelativeLayout 
......FrameLayout 
......FrameLayout 
......TextView 
...RelativeLayout 
......Button 

Der zweite RelativeLayout hat nur einen Knopf und einen transparenten Hintergrund. Mein vollständiger XML-Code ist unten als Referenz.

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/main_root_view" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#eee" 
     tools:context="com.wecancer.wecancer.activities.Menu"> 

     <FrameLayout 
      android:layout_alignParentBottom="true" 
      android:id="@+id/main_bottom_menu_container" 
      android:layout_width="match_parent" 
      android:background="@color/lightGray" 
      android:layout_height="wrap_content" /> 

     <FrameLayout 
      android:id="@+id/main_container_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="0dp" /> 

     <TextView 
      android:id="@+id/main_center_tv" 
      android:layout_centerVertical="true" 
      android:textSize="24sp" 
      android:layout_centerHorizontal="true" 
      android:text="@string/menu" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:onClick="menuPlusBt" 
      android:elevation="5dp" 
      android:id="@+id/menu_plus_img" 
      android:layout_centerHorizontal="true" 
      android:layout_alignParentBottom="true" 
      android:background="@drawable/green_circle" 
      android:layout_width="70dp" 
      android:layout_marginBottom="0dp" 
      android:layout_height="70dp" 
      tools:targetApi="lollipop" /> 

    </RelativeLayout> 

</FrameLayout> 
Verwandte Themen