安卓开发学习笔记(二)

六、使用容器

1.常用容器:LinearLayout(盒模型),RelativeLayout(规则模型),TableLayout(网格模型),ScrolView(用来辅助实现滚动容器的模型).
2.LinearLayout:线性布局
可以控制它的5个方面:
(1)方向:
android:orientation=”horizontal” //表示行
android:orientation=”vertical” //表示列
LinearLayout a;
a.setOrientation(value); //设置行列,value=LinearLayout.HORIZONTAL或LinearLayout.VERTICAL
(2)填充模型:
android:layout_width //填充宽度
android:layout_height //填充高度
它们的值可能是3种形式:
“125px” 表示固定125像素
“wrap_content” 表示部件应该保持本来的大小,如果太大则会自动换行
“fill_parent” 表示在处理完其他所有的部件之后当前部件应该填满包含它的容器的可用空间
(3)权重
由 fill_parent引出,如果两个部件共享空间怎么办?
所以必须在后面加入 :android:layout_weight
部件默认权重为0,如果一个部件设置为1,另一个部件设置为2,则第二个部件拥有空间就是第一个部件的两倍。除此之外还可以用百分比为单位: android:layout_width=0
android:layout_weight=50 //此部件占比50%
(4)重力(对齐方式)
android:layout_gravity
LinearLayout a;
a.setGravity(value); //设置行列,value=left,center_horizontal,right
(5)内边距(单位px)
四边边距相同
android:padding
LinearLayout a;
a.setPadding(value);
四边边距不同
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBotton
如果想设置外边距可以使用:android:layout_marginTop
3.RelativeLayout:相对布局
(1)相对于容器定位
android:layout_alignParentTop //将部件的顶部与容器的顶部对齐
android:layout_alignParentBottom //将部件的底部与容器的底部对齐
android:layout_alignParentLeft //将部件的左侧与容器的做左侧对齐
android:layout_alignParentRight //将部件的右侧与容器的右侧对齐
android:layout_centerHorizontal //将部件在容器内水平居中
android:layout_centerVertical //将部件在容器内垂直居中
android:layout_centerInParent //将部件在容器内水平和垂直居中
所有属性都是boolean,对齐时内边距也会算在内
(2)特性中的关联记号
通过id来关联
(3)相对其他部件定位
控制定位
android:layout_above //在上方
android:layout_below //在下方
android:layout_toLeftOf //在左侧
android:layout_toRightOf //在右侧
控制对齐
android:layout_alignTop //当前部件的上沿应与引用部件上沿对齐
android:layout_alignBottom //当前部件的下沿应与引用部件下沿对齐
android:layout_alignLeft //当前部件左边应与引用部件左边对齐
android:layout_alignRight //当前部件右边应与引用部件右边对齐
andriod:layout_alignBaseline //表示两个部件应该沿基线对齐(文本对齐线)
(4)求值次序
4.TableLayout:表格布局
(1)在行中添加单元格
TableLayout需要和TableRow一起配合使用,TableLayout控制容器的总体行为,而部件则放入网格中一行一个的TableRow中,最少一个部件一列,列数由最多部件的行的列数决定,列从0开始计数。
android:layout_span=”3” //可以控制一个部件所占的列数
android:layout_column=”2” //部件会被放到号数为2的列,即第3列
(2)TableLayout的其他子元素
通常TableLayout中只包含TableRow作为直接子元素,如果要放置其他东西的话可以用View
例:
View android:layout_height=”2px”
android:background=”#0000FF” (加入 < />)
(3)扩展、收缩、折叠(在TableLayout中)
android:stretchColumns=”3” //将第4列的部件扩展占据可用空间
android:shrinkColumns=”3” //将第4列的部件进行换行处理
android:collapseColumns=”3” //将第4列的部件折叠(还是表格的一部分,只是不可见)
5.ScrollView:滚动
就是在原来的layout上套一个…….

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值