如果我有 2 个视图,我想要一个占用必要的空间,另一个占用剩余的空间,我应该怎么做?
假设我想垂直放置视图。下面的视图应该占用必要的空间(wrap_content),上面的视图应该占用容器布局的剩余空间。
我使用了这两个解决方案(简化代码):
1)LinearLayout与weight
<LinearLayout ...>
<View height="0dp" weight="1" .../>
<View height="wrap_content" .../>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
2)RelativeLayout对齐
<RelativeLayout ...>
<View height="wrap_content" id="@+id/big" alignParentTop .../>
<View height="wrap_content" below="@+id/big" alignParentBottom .../>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
该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>
Run Code Online (Sandbox Code Playgroud)
我会使用相对布局:
<View height="match_parent" id="@+id/big" android:layout_above="@+id/view2" alignParentTop ... >
<View height="wrap_content" id="@+id/view2" alignParentBottom ...>
Run Code Online (Sandbox Code Playgroud)
这样第一个视图将填充屏幕高度,直到它碰到第二个视图。
| 归档时间: |
|
| 查看次数: |
4997 次 |
| 最近记录: |