2013-05-20 11 views
27

Ich programmiere programmatisch in Android. Übergeordnetes Layout ist relatives Layout und ich füge zwei Ansichten hinzu. Die erste ist die Listenansicht und die zweite ist die benutzerdefinierte Ansicht. Ich möchte, dass meine zweite benutzerdefinierte Ansicht oben auf der Liste steht, aber irgendwie wird sie hinter der Listenansicht versteckt. Wie stelle ich sicher, dass meine benutzerdefinierte Ansicht oben ist?Wie man eine Ansicht über andere in Relative Layout programmatisch in Android setzen?

+1

bitte postyour Code –

Antwort

11

Versuchen zu verwenden: View.bringToFront();

+0

seine nicht funktioniert, ich weiß nicht, warum – Chimu

+0

Dank .. its arbeiten für mich .. :) – ADT

4

Verwenden FrameLayout, Frame-Layout zuletzt hinzugefügt Ansicht oben zeigt ..

+1

Ich will nicht FrameLayout verwenden, isn Gibt es das überhaupt im Relativen Layout? – Chimu

4

Versuchen:

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

    <ListView 
    android:id="@+id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

    <YourCustomView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" /> 

</RelativeLayout> 
+1

Aus irgendeinem Grund möchte ich Relative Layout nur verwenden, ist nicht da, um z-Reihenfolge der relativen Layout Kinder zu behandeln – Chimu

1

Nochmal: Blick auf das Layout in der Sie es hinzufügen Bestellung erhalten. Frage ist: ist es wirklich versteckt oder macht es nicht ?. Überprüfen Sie LogCat und Console auf Fehler. Wenn Sie nur diese einzelne benutzerdefinierte Ansicht zu Ihrem Layout hinzufügen, wird es ohne Probleme gerendert? Wenn dies der Fall ist, stellen Sie sicher, dass Sie Ihre benutzerdefinierten Daten der gleichen übergeordneten Ansicht (Gruppe) hinzufügen, wenn Sie Ihre ListView hinzufügen. Wenn Sie nicht weiter kommen, geben Sie das entsprechende Codebeispiel an.

6

Lösung 1 - View # BringToFront() View.bringToFront() sollte funktionieren, wenn sie ordnungsgemäß verwendet werden. Vielleicht haben Sie vergessen diese (Quelle: http://developer.android.com/reference/android/view/View.html#bringToFront()):

Vor KITKAT diese Methode durch Aufrufe requestLayout() und ungültig machen()

Lösung 2 gefolgt werden sollte - wieder anbringen Ansicht

Wenn View.bringToFront() nicht funktioniert, versuchen Sie, die Ansicht erneut zu entfernen und hinzuzufügen. Der Vorteil ist, dass der gleiche Code auch bei Versionen vor KITKAT verwendet wird.

ich es auf diese Weise in meinem Code tue:

ViewGroup parentView = (ViewGroup) (listView.getParent()); 
parentView.removeView(addFab); 
parentView.addView(addFab); 
0

Dies funktioniert für mich:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="220dp" 
android:gravity="bottom" 
android:orientation="vertical" 
android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

<ImageView 
    android:id="@+id/details_wrapper" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingBottom="16dip" 
    android:scaleType="centerCrop" 
    android:src="@drawable/temp_nav_image"> 
</ImageView> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Kapil Rajput" 
     android:textStyle="bold" 
     android:textColor="@android:color/black" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1"/> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:textStyle="bold" 
     android:text="[email protected]"/> 
</LinearLayout> 
</RelativeLayout> 
0

ich relativ Layout und die Reihenfolge der Ansichten irgendwie bestimmt verwendet, die eine oben ist .

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 
    <SeekBar 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:splitTrack="false" 
     android:layout_alignParentTop="true" 
    /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"/> 

In diesem Layout wird die Textview auf die

0

Make geordneten Ansicht als relatives Layout seekbar sein und Ihren beide Blick in xml.It gewohnt für Linear-Layout arbeitet hinzuzufügen. Und dann fügen Sie FirstView.bringToFront() programmatisch hinzu.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@mipmap/bg" 
android:orientation="vertical" 
android:padding="15dp"> 


<ImageView 
    android:id="@+id/imgIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_gravity="center" 
    android:layout_marginBottom="-30dp" 
    android:layout_marginTop="30dp" 
    android:src="@mipmap/login_logo" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/imgIcon" 
    android:layout_gravity="bottom" 

    android:background="#B3FFFFFF" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp"> 

<View 1/> 
<View 2/> 
</LinearLayout> 

Java file 
imgIcon.brigtToFront(); 
Verwandte Themen