安卓中四种常见布局的区别与特点:

相对布局(relativelayout)
      在此布局容器中,子组件的位置总是相对于兄弟组件和父容器来决定的,并且相对布局一定要加id才能管理。

RelativeLayout中子控件常用属性:

1、相对于父控件,例如:android:layout_alignParentTop=“true”

android:layout_alignParentTop 控件的顶部与父控件的顶部对齐;

android:layout_alignParentBottom 控件的底部与父控件的底部对齐;

android:layout_alignParentLeft 控件的左部与父控件的左部对齐;

android:layout_alignParentRight 控件的右部与父控件的右部对齐;

2、相对给定Id控件,例如:android:layout_above=“@id/**”

android:layout_above 控件的底部置于给定ID的控件之上;

android:layout_below 控件的底部置于给定ID的控件之下;

android:layout_toLeftOf 控件的右边缘与给定ID的控件左边缘对齐;

android:layout_toRightOf 控件的左边缘与给定ID的控件右边缘对齐;

android:layout_alignBaseline 控件的baseline与给定ID的baseline对齐;

android:layout_alignTop 控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom 控件的底部边缘与给定ID的底部边缘对齐;

android:layout_alignLeft 控件的左边缘与给定ID的左边缘对齐;

android:layout_alignRight 控件的右边缘与给定ID的右边缘对齐;

3、居中,例如:android:layout_centerInParent=“true”

android:layout_centerHorizontal 水平居中;
android:layout_centerVertical 垂直居中;
android:layout_centerInParent 父控件的中央;
使用案例如图1所示
图一
代码实现:

android:id="@+id/button_center"
android : layout_width="wrap_content"android : layout_height="wrap_content"android : layout_centerInParent="true"android:text="center" / >
<Button
android: id="@+id/ button_above"
android : layout_width="wrap_content"android : layout_height="wrap_content"
android : layout_above="@+id/button_center"android : layout_centerInParent="true"
android:text="above" / >
<Button
android:id="@+id/button_below"
android: layout_width="wrap_content"android : layout_height="wrap_content"
android : layout_below="@+id/button_center"android : layout_centerInParent="true"
android :text="below" / >
<Button
android:id="@+id/ button_left"
android : layout_width="wrap_content"android : layout_height= "wrap_content"
android : layout_toLeftOf="@+id/ button_center"android : layout_centerVertical="true"
android:text="ieft" />
<Button
android: id="@+id/ button_right"
android : layout_width="wrap_content"android : layout_height="wrap_content"
android : layout_toRightOf="@+id/ button_center"android: layout_centerVertical="true"
android:text="right" />
</RelativeLayout>

线性布局(linearlayout)
      就是将容器中的组件一个一个的排列起来,通过设置属性来控制组件的横向或纵向排列。
常见属性值:

android:gravity:内部控件对齐方式,常用属性值有(center、center_vertical、center_horizontal、top、bottom、left、right等)。
“android:orientation”控制方向,属性值垂直(vertical)和水平(horizontal),默认水平方向。
android:layout_weight,为容器内的组件设置权重,表示当所有控件全部排列完毕后,该被设置的组件占父容器剩余空白部分的多少比重
android:layout_gravity,为容器内的控件设置该控件在父容器中的对齐方式

当父容器线性设置为 vertical 纵向时,则只有设置与左右相关的值才起作用,left、right
当父容器线性设置为 horizontal 横向时,则只有设置与上下相关的值才起作用,top、bottom
使用案例如图2所示
在这里插入图片描述
代码实现:

<!--线性布局中嵌套布局,LinearLayout vertical纵向排列-->
<LinearLayout
android: layout_width="match_parent"android:layout_height="wrap_content"android : layout_weight="1"
android:orientation="vertical">
<Button
android : layout_width="wrap_content"android: layout_height="wrap_content"android:text="1.1第一层按钮"
/>
<Button
android: layout_width="2oodp"
android:layout_height="wrap_content"android : layout_gravity="right"
android:gravity="left | bottom"android:text="1.2第二层按钮"

帧布局(FrameLayout)
      其中的Frame的坐标原点是屏幕的左上角,位置固定,只需为控件指定大小即可。
此时,所出现的内容,是全部重叠在一起的,如果要设置不同位置,可采用android:layout_gravity属性,常见属性值(top,bottom,left,right,center_vertical,center_horizontal)
在这里插入图片描述
表格布局(tablelayou)
      表格布局继承了线性布局,采用行和列的形式来管理子组件,且不需要声明有多少行与列,只需要添加tablerow和组件来控制行与列的数量。
常见属性:
android:stretchColumns,表示被设置的这些列可拉伸(列号从0开始计算)
android:shrinkColumns,表示被设置的这些列可收缩
android:layout_column,指定该控件在TableRow中的指定列
android : stretchcolumns=“e,2"android: shrinkcolumns=”"1”
android:collapsecolumns=“2”>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值