2017-05-03 3 views
-1

sagen, dass ich die folgende XMLWie positioniere ich eine Ansicht rechts neben dem Kind eines Geschwisters? Android XML

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

     <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 
      <RelativeLayout 
          android:id="@+id/child1" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="2"> 

       <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:textSize="15sp"/> 

      </RelativeLayout> 

      <RelativeLayout 
         android:id="@+id/child2" 
         android:layout_width="0dp" 
         android:layout_height="match_parent" 
         android:layout_weight="2"> 
      </RelativeLayout> 

     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:layout_toEndOf="@+id/child1"/> // CAN'T DO THIS need work around 

    </RelativeLayout> 

Ich habe etwas Ähnliches wie dieses haben. Ich habe eine LinearLayout mit vielen Kindern in einem RelativeLayout eingeschlossen, und ich habe eine View, die child1 und child2 überlappt. Aber ich möchte, dass es nur Kind 2 überlappt.

Gibt es eine Umgehungslösung? Ich kann meinen tatsächlichen Code bei Bedarf veröffentlichen, schrieb irgendwie das im laufenden Betrieb.

Antwort

0

child1 ist nicht im selben Layoutbereich wie die Ansicht, in der Sie es verwenden möchten (an der Zeile "// kann das nicht").

Ich würde vorschlagen, das lineare Layout zu entfernen und sowohl child1 als auch child2 im übergeordneten relativen Layout zu haben.

+0

Ja, aber dann werde ich nicht in der Lage sein, die inneren Kinder durch Gewichte zu trennen, ich müsste hartcodierte Breite verwenden. – Pants

0

Es ist, weil Kind nicht sein Geschwister ist, Linearlayout ist sein Geschwister, wenn Sie es nach rechts verschieben möchten, versuchen Sie, eine ID auf das lineare Layout und verwenden Sie es auf Android: layout_toEndOf = "@ + id/child1"

Versuchen Sie folgendes:

<?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="wrap_content"> 

    <LinearLayout android:id="@+id/ll_sibbling_view" 
     android:background="@color/color_gray_400" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <RelativeLayout 
      android:id="@+id/child1" 
      android:background="@color/colorAccent" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="2"> 

      <TextView android:text="ABC" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15sp"/> 

     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/child2" 
      android:background="@color/mab_blue" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="2"> 
     </RelativeLayout> 

    </LinearLayout> 

    <View android:id="@+id/v_center_horizontal" 
     android:layout_centerHorizontal="true" 
     android:layout_width="0dp" 
     android:layout_height="0dp"/> 

    <View android:layout_toRightOf="@+id/v_center_horizontal" 
     android:layout_toEndOf="@+id/v_center_horizontal" 
     android:background="@color/black" 
     android:layout_width="match_parent" 
     android:layout_height="2dp"/> 

</RelativeLayout> 
+0

Richtig, aber das würde sich nicht überlappen child2 nein? – Pants

+0

Nun, ich habe die Antwort bearbeitet, aber es ist eine andere Art von Lösung, ich hoffe, es könnte Ihnen helfen. – jmarkstar

0

Ok, damit ich diese programmatisch zu tun hatte. Ich die Breite der child1 Spalte berechnet mit

view.post(new Runnable() { 
      @Override 
      public void run() { 
       child1width = child1.getWidth(); 
       setMarkers(); // sets margin of View (the line) 
      } 
     }); 

und dann einen linken Rand von child1Width der Ansicht hinzugefügt.

Verwandte Themen