Android:如何在RelativeLayout中均匀地分配组件?

Android:如何在RelativeLayout中均匀地分配组件?,android,Android,我试图组织3个线性布局,这样一个向左对齐,一个向右对齐,第三个在两个之间,两边的空白量相等。我想我用相对论的方式来完成这个。然后将一个布局设置为alignParentLeft,将第二个布局设置为alignParentRight。但是对于第三个布局,我想设置layout_marginLeft=(RelativeLayout.width-(layout1.width+layout2.width+layout3.width))/2。不过,我不知道如何在xml中实现这一点。有什么建议吗 *编辑。根据建议

我试图组织3个线性布局,这样一个向左对齐,一个向右对齐,第三个在两个之间,两边的空白量相等。我想我用相对论的方式来完成这个。然后将一个布局设置为alignParentLeft,将第二个布局设置为alignParentRight。但是对于第三个布局,我想设置layout_marginLeft=(RelativeLayout.width-(layout1.width+layout2.width+layout3.width))/2。不过,我不知道如何在xml中实现这一点。有什么建议吗

*编辑。根据建议,我现在使用线性布局。这更接近于工作,但3个布局的间距不均匀,并且正确的布局没有正确对齐。下面是我的xml:

<LinearLayout 
        android:id="@+id/toolbar"
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"
        android:paddingTop="4dip"
        android:paddingBottom="4dip"
        android:background="#50000000"
        android:visibility="visible">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
               <ImageButton 
                    android:id="@+id/open"
                    android:src="@drawable/open"
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content">
               </ImageButton>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
            <ImageButton 
                android:id="@+id/prev"
                android:src="@drawable/prev"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
            <TextView
                android:id="@+id/totalpages"
                android:text="0 of 0"
                android:layout_gravity="center"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:textColor="#FFFFFFFF">
            </TextView>
            <ImageButton 
                android:id="@+id/next"
                android:src="@drawable/next"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
            <ImageButton 
                android:id="@+id/zoomout"
                android:src="@drawable/zoomout"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
            <ImageButton 
                android:id="@+id/zoomin"
                android:src="@drawable/zoomin"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
        </LinearLayout>
    </LinearLayout>

使用LinearLayout而不是RelativeLayout,并使用layout\u weight属性。 您可能需要将布局宽度设置为0dip,然后将权重设置为0.33左右

编辑: 是的,您可以使用重量1或低于1的任何其他同等重量。 范例



仅附加到这个答案:使用线性布局(线性布局中的三个线性布局),设置所有宽度以填充父级,并给出内部三个布局
android:layout\u weight=“1”
。谢谢,这让我更接近了,但对我来说并不太有效。这将强制3个线性布局都具有相同的大小,但我的线性布局具有不同的内容。请看我的编辑。你有三个按钮,我猜这三个按钮的宽度不一样。这将是一个棘手的案件。我建议降低图像的大小,这样所有的按钮看起来都很好,不会被裁剪掉。以twitter/facebook应用程序为例。如果不可能,则确定每个按钮的最小宽度,并指定它们[也取决于屏幕大小]。在那之后,我所能建议的就是四处玩玩,看看什么最适合你。我没有看到你编辑的帖子。因此,要使视图右对齐或符合您的要求,您需要在子布局上使用layout_gravity,在父布局上使用gravity,并提及您现在要编辑的内容。由于某些原因,布局图重力:“右”不会强制第三个布局图右对齐。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="string 1 for test...."
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"
            android:text="string 2 for test...."
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
        android:layout_gravity="right"
            android:text="string 3 for test...."
            />
    </LinearLayout>
</LinearLayout>