【发布时间】:2012-09-15 08:37:39
【问题描述】:
我想要实现的是,划分下图中描述的行布局。我应该怎么做才能将行分成3个完全相同的大小和1个未知的大小? X 大小相同,我不知道也不想指定是否不需要...
编辑:按钮位于左侧、中间和右侧。
【问题讨论】:
标签: android listview layout row android-relativelayout
我想要实现的是,划分下图中描述的行布局。我应该怎么做才能将行分成3个完全相同的大小和1个未知的大小? X 大小相同,我不知道也不想指定是否不需要...
编辑:按钮位于左侧、中间和右侧。
【问题讨论】:
标签: android listview layout row android-relativelayout
您的最左侧尺寸是否有最小宽度? 如果是这样,您应该使用水平方向的 LinearLayout。 它可以包含 2 个线性布局,一个包含 3 个视图(您的按钮),宽度为 0,每个权重为 1,另一个线性布局具有 minimumWidth 集。 您可以为第一个布局指定宽度,而不是 marginRight。
Ted Hopp 做对了 ;)
【讨论】:
LinearLayout 不会扩展以填充可用空间。也不需要嵌套LinearLayouts;所有四个按钮都可以放在一个 LinearLayout 中。
您可以使用 layout_weight 属性。为所有 x 布局赋予相同的权重,为问号赋予不同的权重,直到屏幕按照您的喜好划分
【讨论】:
您可以使用android:layout_weight 按比例分配额外空间。您希望左侧的三个按钮吸收所有额外的宽度,因此右侧(第四个)按钮的默认权重应为 0。由于您还希望它们具有相同的宽度,最简单的方法是为它们分配 0dp 的宽度和赋予它们相同的权重:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
【讨论】:
在RelativeLayout 中使用LinearLayout。将 3 个项目放入 LinearLayout 并给它们相同的 weight。在RelativeLayout的帮助下,将未知项目放在LinearLayout的右侧。
左边的元素会根据右边的宽度对齐自己。 这是代码:https://gist.github.com/3772838
这里有两个不同大小的最右边元素的屏幕截图:
科莱凝胶 =)
【讨论】:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/rlayoutParent">
<RelativeLayout
android:layout_width="wrap_content"
android:id="@+id/rlayoutButtons" android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_toRightOf="@+id/button1"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_toRightOf="@+id/button2"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlayoutOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/rlayoutButtons" android:gravity="right">
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
【讨论】: