【发布时间】:2015-12-01 06:25:33
【问题描述】:
如果我有 2 个视图,我希望一个占用必要的空间,另一个占用剩余的空间,我应该怎么做?
假设我想垂直放置视图。下面的视图应该占用必要的空间(wrap_content),上面的视图应该占用容器布局的剩余空间。
我用过这两种解决方案(简化代码):
1) LinearLayout 与 weight
<LinearLayout ...>
<View height="0dp" weight="1" .../>
<View height="wrap_content" .../>
</LinearLayout>
2) RelativeLayout 对齐
<RelativeLayout ...>
<View height="wrap_content" id="@+id/big" alignParentTop .../>
<View height="wrap_content" below="@+id/big" alignParentBottom .../>
</RelativeLayout>
LinearLayout 方法始终有效,RelativeLayout 通常按预期工作,但这显然是模棱两可的,因为没有人说@+id/big 视图应该大于下面的视图。
我认为第一种方法更好,因为它不是模棱两可的。但是,我已经看到了很多关于第二种方法的示例和答案。
对于这些情况,您使用什么解决方案。有最佳做法吗?
谢谢!
编辑
接受Touf 的回答,现在我会这样做(注意match_parent):
<RelativeLayout ...>
<View height="match_parent" id="@+id/big" alignParentTop .../>
<View height="wrap_content" below="@+id/big" alignParentBottom .../>
</RelativeLayout>
【问题讨论】: