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

8
我正在尝试组织3个LinearLayout,其中一个左对齐,一个右对齐,第三个在两者之间具有相等数量的空白空间。我认为我可以将它们包装在RelativeLayout中来实现这一点。然后将一个布局设置为alignParentLeft,将第二个布局设置为alignParentRight。但是对于第三个布局,我想设置layout_marginLeft =(RelativeLayout.width-(layout1.width + layout2.width + layout3.width))/ 2。虽然我不知道如何在xml中实现这一点。有什么建议吗?
*编辑。根据建议,我现在正在使用LinearLayout。这更接近实现,但是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>
1个回答

13

使用LinearLayout而非RelativeLayout,并使用layout_weight属性。您可能需要将layout_width设置为0dip,然后将权重设置为0.33。

编辑: 是的,您可以使用权重1或任何小于1的相等权重。 示例:

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

谢谢,这让我更接近目标了,但对我来说还不太适用。这会强制使3个线性布局的大小都相同,但我的线性布局具有不同的内容。请查看我的编辑。 - ab11
我没有看到你编辑过的帖子。所以,为了使视图右对齐或者任何你想要的效果,你需要在子布局上使用layout_gravity,在父布局上使用gravity,并说明你想支持的所有内容。 - achie
我刚刚测试了一下,它适用于我的文本视图。所以你能否检查一下你是否在父级上使用了其他重力字段或者做了其他的事情。我还修改了我的布局以反映我的布局。 - achie
我相信它对你的TextView有效的原因是你在TextView级别上添加了布局参数。而且你的线性布局都占据了屏幕的三分之一。所以,TextView在其LinearLayout中得到了适当的对齐,这看起来正确,因为LinerLayout很大。但是这对于我的可变大小的LinearLayouts不起作用。我困惑的是为什么在我的LinearLayouts中添加android:layout_gravity无效。 - ab11
不,那也可以。但问题在于,尽管您正在将线性布局本身右对齐,但位于该布局内部的imagebutton并没有右对齐。因此,由于您的内部线性布局占据了屏幕的1/3,所以看起来布局并没有右对齐。因此,请更改线性布局内部的imagebutton的layout_gravity。 - achie
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接