android:layout_gravity 指的是本元素相对于父元素的重力方向
如下布局:
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:background="#ffff00" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:text="习近平" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:text="李克强" /> </LinearLayout>
显示的效果为:
我们是在LinearLayout中设置了android:layout_gravity,这个属性是指定本元素相对于父元素的位置,因为该布局已经是根布局,它的父元素就是整个屏幕,所以这个元素设置的是LinearLayout相对于屏幕的位置【图中黄色区域】。
android:gravity 指的是本元素所有子元素的重力方向
现在我们在LinearLayout中添加android:gravity="bottom|left"属性
代码如下:
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:gravity="bottom|right" android:background="#ffff00" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:text="习近平" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:text="李克强" /> </LinearLayout>
效果显示:
从结果可以看出,android:gravity属性改变的是本元素中所有子元素的位置。
android:layout_weight 指的是线性布局内子元素对未占用空间(水平或垂直)分配权重值,存在两种情况:
1:子元素水平方向设置了android:layout_width="fill_parent"属性或者是垂直方向设置了android:layout_height="fill_parent"属性时,权重分配值越小,权重就越大!
2:子元素水平方向设置了android:layout_width="wrap_content"属性或者是垂直方向设置了android:layout_height="wrap_content"属性时,权重分配值越小,权重就越小!
如下代码:
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:gravity="bottom|right" android:background="#ffff00" android:orientation="horizontal" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:layout_weight="1" android:text="习近平" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:layout_weight="2" android:text="李克强" /> </LinearLayout>
效果显示:
从图中效果可以看出:子元素水平方向设置了android:layout_width="fill_parent"属性时权重分配值越小,权重就越大!
如下代码:
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center" android:gravity="bottom|right" android:background="#ffff00" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:layout_weight="1" android:text="习近平" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#00ffff" android:layout_weight="2" android:text="李克强" /> </LinearLayout>
显示效果:
从图中效果可以看出:子元素水平方向设置了android:layout_width="wrap_content"属性时,权重分配值越小,权重就越小!
另外:android:orientation属性 指的是线性布局以行或列来显示其内部子元素
android:orientation="vertical" 垂直排列即每个子元素各占一行,从上到下进行排列。
android:orientation="horizontal" 水平排列即每个子元素各占一列,从左往右进行排列。