我在android中有两个线性(也尝试过相对)布局的案例。一个用于水平方向,另一个用于垂直方向。让我们从水平开始:
它有点像:
<LinearLayout ... >
<Button ... layout:gravity = "left" layout:width = "wrap_content"/>
<TextView ... layout:width = ??????? />
<Image .... layout:gravity = "right" layout:width = "wrap_content"/>
</LinearLayout>
好吧,我希望按钮保持在左边,图像保持在右边(保持在文本视图的末尾,而不仅仅是文本视图的右边),文本视图(可能带有自动宽度或其他)保持在在中间。如果我输入textview width="fill/match_parent,它会将图像发送到屏幕外。如果我输入wrap_content,则图像不会停留在屏幕右侧。我也尝试过相对布局,但没有成功。
垂直方向也是如此,我有类似的东西:
<LinearLayout ...>
<LinearLayout .... layout:height = "wrap_content" layout:gravity= "top" />
<ListView layout:height = ???????>
<LinearLayout ... layout:height = "wrap_content" layout:gravity = "bottom" />
</LinearLayout>
此处的要求相同。我希望第一个L布局保持在顶部,列表视图在它们之间自动调整大小,第二个线性布局保持在底部。(想象一下,我试图在iPhone中创建一个看起来像UITableView的视图,该视图底部有一个NavigationBar、项目列表和一个ToolBar。第一个LinearLayout是NavigationBar,第十一个视图是单元格,第二个LinearLayout是ToolBar)。
有什么建议吗?更喜欢xml解决方案。
只需使用RelativeLayout即可完成。
水平对齐
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="something"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/image"
android:layout_toRightOf="@+id/button" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/icon" />
</RelativeLayout>
垂直对齐
<LinearLayout
android:id="@+id/linear_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linear_top"
android:layout_above="@+id/linear_bottom" />
<LinearLayout
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
对于您的第二个需求,请使用列表视图的layout_weight
<RelativeLayout ...>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentTop="true" />
<ListView
android:layout_height ="0dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_below="@+id/top_layout"
android:layout_above="@+id/bottm_layout" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true" />
</RelativeLayout>
您可以尝试TableLayout,而Stretch Column的LinearLayout为1(表LAyout中的列以0为基数进行索引)。
第一列是您的按钮。
第二列是您的文本视图。
第三列是您的图像。
您可以在谷歌上搜索示例或查看TableLayout。
更新:
你可以更清楚地看到:
<RelativeLayout>
<!-- This is Header-->
<LinearLayout
.....
android:layout_alignParentTop="true" >
</LinearLayout>
<!-- This is your table-->
<TableLayout
.....
android:stretchColumns = 1>
<TableRow>
<Button>
.....
</Button>
<TextView>
.....
</TextView>
<ImageView>
.....
</ImageView>
</TableRow>
</TableLayout>
<!-- This is Footer-->
<LinearLayout
.....
android:layout_alignParentBottom="true" >
</LinearLayout>
</RelativeLayout>