Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is android:layout_gravity="clip_vertical" exactly

Tags:

android

The property android:layout_gravity="clip_vertical|horizontal" does the following as mentioned in the SDK documentation:

Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.

But I can't see anything of this in my applications,

so what is the purpose of this property exactly ?

thanks

like image 914
Mina Wissa Avatar asked Nov 16 '10 19:11

Mina Wissa


People also ask

What is the use of layout_gravity in android?

The android:layout_gravity is an attribute that sets the gravity of the View or Layout in its parent. The android:layout_gravity is used to control the gravity of an individual view in a container. It is the outside gravity of the View. It specifies the direction in which the View should touch it's parent's border.

What is the difference between android gravity and android layout_gravity property?

So in general android:layout_gravity attribute is used by child views to tell their parent how they want to be placed inside it, while android:gravity is used by the parent layout to tell the child views how they should be placed inside it.

What is gravity in linear Layout?

XML attributes Drawable to use as a vertical divider between buttons. android:gravity. Specifies how an object should position its content, on both the X and Y axes, within its own bounds.


1 Answers

Short version:

clip_horizontal and clip_vertical apply to the measurements of the view itself, before any contents (such as the image in a BitmapDrawable) are rendered.


Long version:

I've run into some similar confusion over clip_horizontal and clip_vertical. (In my case, it was related to android:gravity for a BitmapDrawable, but it's similar enough to be applicable.)

From the documentation I thought that something like android:gravity="top|left|clip_vertical" on a bitmap would cause the image's top left corner to be positioned at the view's top left corner, and that, if the bitmap was taller than the view, it would be "clipped" at the bottom edge of the view. In other words, show only as much of the bitmap that the view is tall enough to reveal; do not stretch the bitmap, but instead only show whatever will fit, letting the rest extend below the bottom edge.

However, the opposite happened: when I set clip_vertical, a large bitmap was squished vertically to fit within the height of the view.

After examining the applyDisplay() method in platform/frameworks/core/java/android/view/Gravity.java, I realized my mistake:

It isn't the bitmap image that was going to be clipped, but the view -- the actual size of the container the image is ultimately rendered into.

Setting clip_vertical in my case didn't mean "clip the image at the bottom edge," it meant "clip the BitmapDrawable's view itself so its height matches the height of its parent container"...which then caused the image to be "squished" as it filled that shorter height.

So, the important thing to remember with android:gravity and android:layout_gravity is that clip_horizontal and clip_vertical apply to the measurements of the view itself, before any contents (such as my BitmapDrawable) are rendered.

like image 125
Lorne Laliberte Avatar answered Sep 19 '22 15:09

Lorne Laliberte