2010-04-12 119 views
2

我想知道如何获得这样的结果(在图片链接):如何做到这一点布局?

http://img718.imageshack.us/img718/6173/screenshot20100411at126.png

这是我的尝试:

<RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="#CCCCCC" 
        android:orientation="horizontal" 
        android:layout_weight="0"> 

     <Button android:id="@+id/btn_cancel" 
       android:text="@string/cancel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="5px" 
       android:gravity="left" 
       /> 

     <Button android:id="@+id/btn_save" 
       android:text="@string/save" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="5px" 
       android:layout_toRightOf="@id/btn_cancel" 
       android:gravity="right"/> 
    </RelativeLayout> 

我试图与layout_width性能发挥和设置起来以填补他们两个人的父母,但没有工作,如果任何人有想法...谢谢!

+1

你的问题在这里回答:http://stackoverflow.com/questions/2127459/which-android-control-to-use – 2010-04-12 06:38:13

+0

好吧,我去检查一下! – Spredzy 2010-04-12 07:39:30

回答

4
<LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dip" 
       android:background="#CCCCCC" 
       android:orientation="horizontal"> 

    <Button android:id="@+id/btn_cancel" 
      android:text="@string/cancel" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dip" 
      android:layout_weight="1"/> 

    <Button android:id="@+id/btn_save" 
      android:text="@string/save" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 
</LinearLayout> 

在LinearLayout中的手段来得到两个widget占用的空间相同数量是设置在两者layout_width="fill_parent",然后将他们两个layout_weight="1"为好。设置为fill_parent和相同的layout_weight将告诉LinearLayout分离两者之间的所有可用空间。

此外,使用dip,而不是pxdip独立于屏幕尺寸,并且在不同尺寸的屏幕上看起来会更好。

+0

谢谢你的代码和下面的解释。很高兴知道。 – Spredzy 2010-04-12 07:39:03