CooL i3oY
CooL i3oY

Reputation: 800

what's different between layout_gravity="top" and android:layout_alignParentTop="true"?

I wanna put image in top of View and a listview bottom of it. what's best and correct way?

LinearLayout?RelativeLayout? and with which attribute?

layout_gravity="top"?
layout_alignParentTop="true"?

please give me a snipped code and a brief description about:

what's different between layout_gravity="top" and android:layout_alignParentTop="true"?

Upvotes: 2

Views: 6950

Answers (1)

user
user

Reputation: 87064

I wanna put image in top of View and a listview bottom of it. what's best and correct way?

If you want to place a ListView below an ImageView positioned at the top of the current view then you could use both layouts, it isn't any real difference.

The layour_gravityis used to place the children relative within its parent bounds(the Relativelayout doesn't have this attribute). For example you could use a LinearLayout with orientation vertical which will stack your two children one on top of the other like you want. Also layout_gravity="top" is ignored for a vertical orientated LinearLayout as it doesn't make sense, so you could remove it from the layout completely:

<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 is a placement rule for children of RelativeLayout(only for this type of layout!) which tells them to position aligning the top of the children with the top of the parent RelativeLayout. In this case, to stack the children you would do:

<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>

Upvotes: 2

Related Questions