2016-04-22 61 views
1

有沒有辦法將佈局與另一個佈局的子對齊? 我已經創建了一個例子:將佈局與另一個佈局的子對齊

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

    <RelativeLayout 
     android:id="@+id/rel1" 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:background="#7F000000" 
     android:gravity="center" 
     android:layout_alignParentEnd="true"> 
     <View 
      android:id="@+id/view" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_marginBottom="5dp" 
      android:background="#00FF00"/> 
     <View 
      android:id="@+id/view2" 
      android:layout_width="50dp" 
      android:layout_below="@+id/view" 
      android:layout_height="50dp" 
      android:background="#00FF00"/> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/rel2" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="#7F0000FF" 
     android:layout_alignTop="@+id/view2"> 

    </RelativeLayout> 
</RelativeLayout> 

在這裏,我嘗試對準RelativeLayout的REL2與視圖2的頂部上方,但RelativeLayout的不支持這樣做。

我:

enter image description here

我想:

enter image description here

感謝。

+1

只需**不嵌套**其他2個RelativeLayouts的主要原因之一。完善。和**更快**。 –

+0

你想rel2的寬度匹配父母或寬度rel1。? –

+0

你有沒有在下面檢查http://stackoverflow.com/a/36837747/2826147答案? –

回答

1

你不行。在給定的RelativeLayout

佈局約束應該是同一個相對佈局內的其他 意見(但不是自己!)

0

我敢肯定,你不能這樣做,從XML文件,但我相信,你可以在運行時設置rel2LayoutParams匹配的view2LayoutParams,並設置根佈局FrameLayout

0

採取的LinearLayout父比使用兩RelativeLayout的在兒童,

使用android:weightSum的LinearLayoutandroid:orientation

現在,看看這個XML,你會得到你所需要的輸出

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

    <RelativeLayout 
     android:id="@+id/rel3" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1.4" 
     android:gravity="center"> 

     <RelativeLayout 
      android:id="@+id/rel21" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_marginTop="55dp" 
      android:background="#7F0000FF" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/rel1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.6" 
     android:background="#7F000000" 
     android:gravity="center"> 

     <View 
      android:id="@+id/view" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="5dp" 
      android:background="#00FF00" /> 

     <RelativeLayout 
      android:id="@+id/rel2" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_below="@+id/view" 
      android:background="#7F0000FF"> 

      <View 
       android:id="@+id/view2" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:layout_centerHorizontal="true" 
       android:background="#00FF00" /> 
     </RelativeLayout> 
    </RelativeLayout> 

</LinearLayout> 

enter image description here

+0

對不起,我可能是一個解決方案,但它沒有我想象的那麼靈活。 例如,如果添加一個新的綠色正方形,或者如果更改了layout_marginBottom,則會鬆開水平的藍色線條對齊。 我希望有一個藍色線,它的位置完全取決於綠色的方形位置。 –

+0

我不知道,你做了什麼後,你想做的。我們通常回答所要求的問題。 –

0
<?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"> 

<RelativeLayout 
    android:id="@+id/rel1" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentEnd="true" 
    android:background="#7F000000" 
    android:gravity="center"> 

    <View 
     android:id="@+id/view" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginBottom="5dp" 
     android:background="#00FF00" /> 

    <View 
     android:id="@+id/view2" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_below="@+id/view" 
     android:background="#00FF00" /> 


</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/rel3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"> 

    <RelativeLayout 
     android:id="@+id/rel2" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:layout_marginTop="55dp" 
     android:background="#7F0000FF"> 

    </RelativeLayout> 
</RelativeLayout>