我想将图像放在 View 顶部,将 ListView 放在底部。 什么是最好和正确的方法?
线性布局?相对布局? 以及哪个属性?
layout_gravity="top"?
layout_alignParentTop="true"?
请给我一段代码 fragment 和简要说明:
layout_gravity="top"和 android:layout_alignParentTop="true"有什么不同?
最佳答案
I wanna put image in top of View and a listview bottom of it. what's best and correct way?
如果您想将 ListView
放置在位于当前 View 顶部的 ImageView
下方,那么您可以使用这两种布局,这没有任何真正的区别。
layour_gravity
用于将子级相对放置在其父级边界内(Relativelayout
没有此属性)。例如,您可以使用方向为vertical
的LinearLayout
,它会将您的两个子元素堆叠在一起,如您所愿。另外,对于 vertical
方向的 LinearLayout
来说,layout_gravity="top"
会被忽略,因为它没有意义,因此您可以将其从布局中删除完全:
<LinearLayout android:orientation="vertical">
<!-- the layout_gravity is useless int this case and could be removed-->
<ImageView android:layout_gravity="top"/>
<ListView />
</LinearLayout>
layout_alignParentTop
是 RelativeLayout
的子级的放置规则(仅适用于这种类型的布局!),它告诉它们将子级的顶部与父 RelativeLayout
。在这种情况下,要堆叠子项,您需要执行以下操作:
<RelativeLayout>
<!-- you could remove the layout_alignParentTop attribute because by default the Relativelayout will position it's children there -->
<ImageView android:id="@+id/imageId" android:layout_alingParentTop="true" />
<!-- Position this child below the other -->
<ListView android:layout_below="@id/imageId"/>
</RelativeLayout>
关于android - layout_gravity ="top"和 android :layout_alignParentTop ="true"? 有什么不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12016438/