0

stieß ich auf eine seltsame Frage, unter mein Layout ist:Android layout_below in RelativeLayout

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
> 

<ImageView 
    android:src="@color/colorAccent" 
    android:layout_width="match_parent" 
    android:layout_height="200dp"/> 
<ImageView 
    android:id="@+id/image" 
    android:src="@mipmap/ic_launcher" 
    android:layout_centerInParent="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

<TextView 
    android:text="text" 
    android:layout_below="@+id/image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</RelativeLayout> 

enter image description here

TextView nicht unter ImageView wie erwartet, es ist unterhalb der ursprünglichen Position des ImageView.

Wenn ich layout_height von RelativeLayout zu 200dp oder match_parent setze, funktioniert es gut.

Wie löse ich das?

+0

Nun, es ist ein relatives Layout.Sie müssen angeben, wo jedes Element relativ zu einem anderen steht. Um alle Elemente in einer Zeile oder Spalte auszurichten, verwenden Sie LinearLayout – Imdad

+0

Warum haben Sie die erste Bildansicht hinzugefügt? Wenn Sie die Hintergrundfarbe des Layouts ändern möchten, gibt es einen anderen Weg. –

+0

Verwenden Sie match_parent anstelle von wrap_content zum root-Layout. –

Antwort

0

Einfache Lösung für Sie ist android:layout_height="match_parent" in Ihrem Root-Layout zu verwenden. Wenn Sie wrap_content für Ihr Root-Layout verwenden, funktionieren android:layout_centerInParent und android:layout_below nicht ordnungsgemäß.

Entweder Höhe wie oder statisch verwenden.

Wahl 1:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="200dp"> 

Wahl 2:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

Wahl 3:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="text" /> 
</LinearLayout> 

seine jetzt bis zu Ihnen, die so zu verwenden, Lösung.

0

Wenn Sie Ihre Relative Layout-Wrap Content und Ihre zweite Image view Nehmen mit layout_centerInParent dann müssen Sie marginTop zu Text View geben.

Versuchen Sie mit untenstehenden Code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:src="?attr/colorPrimary" /> 

    <ImageView 
     android:id="@+id/imageTest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:src="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/tvText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageTest" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="80dp" 
     android:text="text" 
     android:textSize="22sp" /> 


</RelativeLayout> 

Hier Screen Shot.

enter image description here

+0

Danke für die Hilfe. – jfxu

+0

@xujifa Bitte upvote und akzeptiere die Antwort, wenn es hilfreich für Sie ist. Vielen Dank :). –

0

Dank für alle beantwortet. Ich habe endlich eine Lösung. Es ist keine sehr gute Lösung, aber es funktioniert gut.

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/background" 
    android:src="@color/colorAccent" 
    android:layout_width="match_parent" 
    android:layout_height="200dp"/> 
<RelativeLayout 
    android:layout_height="warp_content" 
    android:layout_width="match_parent" 
    android:layout_alignTop="@+id/background" 
    android:layout_alignBottom="@+id/background"> 
    <ImageView 
     android:id="@+id/image" 
     android:src="@mipmap/ic_launcher" 
     android:layout_centerInParent="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:text="text" 
     android:layout_below="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 
</RelativeLayout> 
0

Die oberste Antwort ist eigentlich nicht unbedingt wahr. Sie müssen dem Element keinen Rand hinzufügen, damit es unterhalb der Bildansicht liegt. In meiner Situation habe ich ein RelativeLayout mit einem ImageView und einem ListView. Ich wollte, dass ImageView über ListView steht, aber die ListView den Bereich unter dem ImageView abdeckt. Und das habe ich erreicht, indem ich layout_alignParentTop auf ImageView gesetzt und layout_below zum ListView hinzugefügt habe.

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     tools:context="io.menuloop.menuloopandroid.MainActivity"> 
    <ImageView android:layout_alignParentTop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/yourStory" android:src="@drawable/ic_face_black_24dp" /> 
    <ListView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/yourStory" 
      android:layout_centerHorizontal="true" android:id="@+id/listView" /> 
</RelativeLayout>