2016-04-22 62 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>