2013-10-02 26 views
0

我想在一个RelativeLayout里面有两个相同高度的视图在另一个下面。 我不能使用LinearLayout的重量,所以我必须找出另一种方法来做到这一点。android里面的孩子在relativelayout相同的高度

从我谷歌搜索和阅读所以我发现作为一种解决方案,使用锚视图centerHorizo​​ntal在父视图中,并将2个孩子放在一个上面,一个在锚View下面,但它似乎不工作。

到目前为止我的代码是:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="2" 
    android:gravity="top" > 

     <View android:id="@+id/anchor" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_centerHorizontal="true" 
      android:background="#000"/> 


      <RelativeLayout 
       android:id="@+id/toplayout"      
       android:background="#990000"     
       android:layout_above="@+id/anchor" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

      </RelativeLayout> 

      <RelativeLayout 
       android:id="@+id/bottomlayout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/anchor"> 

       <RatingBar 
        android:id="@+id/ratingBar1" 
        android:layout_width="218dp" 
        android:layout_height="wrap_content" /> 

      </RelativeLayout>   


    </RelativeLayout> 

有没有搞错我的代码或我试图解决问题的方法是从一开始就错了吗?

回答

3

你approuch是你设置它的布局是百磅水平修正,但锚点视图,但它应该是中央垂直,也可以使用对齐顶部和底部马赫的意见边缘

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:gravity="top" > 


    <RelativeLayout 
     android:id="@+id/toplayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#990000" 
     android:orientation="horizontal" 
     android:layout_alignParentTop="true" 
     android:layout_alignBottom="@+id/anchor" 
     android:layout_above="@+id/anchor"> 
    </RelativeLayout> 

    <View android:id="@+id/anchor" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_centerVertical="true" 
     android:background="#000"/> 

    <RelativeLayout 
     android:id="@+id/bottomlayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/anchor" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/anchor" > 

     <RatingBar 
      android:id="@+id/ratingBar1" 
      android:layout_width="218dp" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 
</RelativeLayout> 
相关问题