您的位置:首页 > 移动开发 > Android开发

Android gravity 和 layout_gravity区别

2011-02-12 16:21 302 查看
Strictly speaking android:gravity is not a Layout Param. The android:gravity is really an attribute of the View Group. It controls the way the contents of the View Group will be positioned horizontally and vertically.

android:layout_gravity Layout Param

The android:layout_gravity is a Layout Param. Not all View Groups support this Layout Param. See the documentation to find out which Layout Params

are supported by a particular View Group. Linear Layout does support android:layout_gravity Layout Param.

For a horizontal Linear Layout the following values make sense:

* top

* center

* bottom

That is because the children of a horizontal Linear Layout are layed out horizontally one after the other. Only thing can be controlled using the

android:layout_gravity is how a child view is positioned vertically.

In the above example the top Linear Layout is a horizontal Linear Layout. Setting the android:layout_gravity=top on the l_g=top Button has an

effect of positioning it at the top. Setting the android:layout_gravity=center on the l_g=center Button has an effect of positioning it at the

vertical center. Setting the android:layout_gravity=bottom on the l_g=bottom Button has an effect of positioning it at the bottom.

For a vertical Linear Layout the following values make sense:

* left

* center

* right

That is because the children of a vertical Linear Layout are layed out vertically one below the other. Only thing can be controlled using the

android:layout_gravity is how a child view is positioned horizontally.

In the about example the bottom Linear Layout is a vertical Linear Layout. Setting the android:layout_gravity=left on the l_g=left Button has an

effect of positioning it at the left. Setting the android:layout_gravity=center on the l_g=center Button has an effect of positioning it at the

horizontal center. Setting the android:layout_gravity=right on the l_g=right Button has an effect of positioning it at the right.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: