我正在尝试组织 3 个 LinearLayouts,这样一个向左对齐,一个向右对齐,第三个在两者之间,两边的空白量相等。我想我将它们包装在 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>
最佳答案
使用 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>
关于Android:如何在 RelativeLayout 中均匀分布组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4338792/