安卓布局问题
我一生都无法弄清楚这一点。我试图拥有以下布局(垂直):
ImageView(填充所有可用空间) TextView(根据需要占用垂直空间) EditText(根据需要占用垂直空间) 按钮 按钮 按钮(等距)
我有以下布局设置。图像视图当前设置为 200dip。这个值应该是多少才能填充所有可用空间?其他组件应该首先出现,图像视图应该获得剩下的内容。我已经搜索过这方面的例子,但还没有找到任何东西。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_alignParentTop="true" />
<TextView android:id="@+id/lblDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/PhotoDesc"
android:layout_below="@id/imgView" />
<EditText android:id="@+id/edtDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lblDesc" />
<!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="3">
<!-- Take photos button -->
<Button android:id="@+id/btnCamera"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/TakePhotos" />
<!-- Transmit button -->
<Button android:id="@+id/btnUpload"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Upload" />
<!-- Setup button -->
<Button android:id="@+id/btnSetup"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Setup"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
I can't for the life of me figure this out. I am attempting to have the following layout (vertically):
ImageView (filling all available space)
TextView (taking vertical space as needed)
EditText (taking vertical space as needed)
Button Button Button (spaced equally)
I have the following layout setup. The image view is currently set at 200dip. What should this value be to fill all available space? The other components should come first and the image view should get what's left over. I have searched for an example on this and have not found anything yet.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/imgView"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:layout_alignParentTop="true" />
<TextView android:id="@+id/lblDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/PhotoDesc"
android:layout_below="@id/imgView" />
<EditText android:id="@+id/edtDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lblDesc" />
<!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="3">
<!-- Take photos button -->
<Button android:id="@+id/btnCamera"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/TakePhotos" />
<!-- Transmit button -->
<Button android:id="@+id/btnUpload"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/Upload" />
<!-- Setup button -->
<Button android:id="@+id/btnSetup"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Setup"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您所需要的只是一个在
ImageView
上具有适当权重的LinearLayout
来占用剩余空间。带有
LinearLayout
的layout_weight经常被误解。 LinearLayout 分两次测量其子级。首先,根据给定的layout_width或layout_height测量子项,然后在测量完所有子项后,剩余的剩余空间根据其权重进行划分。这意味着两件重要的事情:
layout_width="match_parent"
对于带有orientation="horizontal"
或layout_height="match_parent"
的LinearLayout
对于orientation="vertical"
。) match_parent 将首先生效,消耗所有当前可用空间,并且不为剩余的子项留下任何剩余空间。layout_width
或layout_height
价值。试试这个布局:
Looks like all you need is a
LinearLayout
with appropriate weight on theImageView
to take the leftover space.layout_weight with
LinearLayout
is often misunderstood.LinearLayout
measures its children in two passes. First, children are measured based on the given layout_width or layout_height, then after all children have been measured any extra space left over is divided according to their weights.This means two important things:
layout_width="match_parent"
for aLinearLayout
withorientation="horizontal"
orlayout_height="match_parent"
fororientation="vertical"
.) match_parent will take effect first, consuming all currently available space and leaving none left over for the remaining children.layout_width
orlayout_height
value.Try this layout:
将
RelativeLayout
更改为带有android:orientation="vertical"
的LinearLayout
,然后添加android:layout_weight="1"
代码>到你的ImageView。下面的示例代码。我将ImageView的背景设置为红色来看看效果。
Change your
RelativeLayout
to aLinearLayout
withandroid:orientation="vertical"
, then addandroid:layout_weight="1"
to your ImageView.Example code below. I set the background of the ImageView to the color red to see the effect.