javascript原生移动云编程3 - 比web还简单的页面UI布局

用javascript在码实的云平台上,可以在云里编写原生的移动应用。UI部分要用到Titanium的UI库,这个库从IOS和安卓的原生UI库整合而来,目的是JS可以调用,并且做到跨平台。UI的核心元素是View,用它做页面布局,比起web的CSS实在容易很多,总共就三种,绝对布局、垂直布局、水平布局,分别对应于下面三个截屏:


1、绝对布局 absolute

这是View默认的布局。View方框的左上角是原点,内挂的子View可以用top, left,right, bottom或center来安排位置。下面的代码是绝对布局,蓝色方框box2压在红色方块box1上。

Class.create(Mash5.Widget, {
	initialize : function (config, params) {
		var container = Ti.UI.createView({
			width : Ti.UI.FILL,
			height : Ti.UI.FILL,
			backgroundColor : '#ccc'
		});
		
		this.setContentView(container);
		
		var box1 = Ti.UI.createView({
		    width: '80%',
		    height: '100dip',
		    backgroundColor: '#f44',
		    borderRadius: dipToPx(20),
		    top: '20dip',
		});
		container.add(box1);
		var box2 = Ti.UI.createView({
		    width: '220dip',
		    height: '200dip',
		    top: '100dip',
		    left: '20dip',
		    borderColor: '#44f',
		    borderWidth: '4dip',
		})
		container.add(box2);
	}
})

2、垂直布局 vertical

手机应用主要用竖屏,垂直布局就是为了竖屏排版的方便,例如制作数据录入的表单,数据列表等。布局需要“拼爹”,在父级View,也就是container里声明layout: 'vertical'。于是蓝色矩形box2自动排在红色矩形box1下面,而间距由box2的top来决定,因此这是个相对布局。

Class.create(Mash5.Widget, {
	initialize : function (config, params) {
		var container = Ti.UI.createView({
			width : Ti.UI.FILL,
			height : Ti.UI.FILL,
			backgroundColor : '#ccc',
			layout: 'vertical',
		});
		
		this.setContentView(container);
		
		var box1 = Ti.UI.createView({
		    width: '80%',
		    height: '100dip',
		    backgroundColor: '#f44',
		    borderRadius: dipToPx(20),
		    top: '20dip',
		});
		container.add(box1);
		var box2 = Ti.UI.createView({
		    width: '80%',
		    height: '200dip',
		    top: '10dip',
		    backgroundColor: '#44f',
		})
		container.add(box2);
	}
})

3、水平布局 horizontal

水平布局会把子View在水平方向自动排列,排满时会自动换行。本例的四个方块,一排按屏幕宽度尺寸只能安排下3个(红色、蓝色、绿色),于是第四个方块box4(黄色)就自动编排到下一排。

Class.create(Mash5.Widget, {
	initialize : function (config, params) {
		var container = Ti.UI.createView({
			width : Ti.UI.FILL,
			height : Ti.UI.FILL,
			backgroundColor : '#ccc',
			layout: 'horizontal',
		});
		
		this.setContentView(container);
		
		var box1 = Ti.UI.createView({
		    width: '33%',
		    height: '120dip',
		    backgroundColor: '#f44',
		});
		container.add(box1);
		var box2 = Ti.UI.createView({
		    width: '33%',
		    height: '120dip',
		    backgroundColor: '#44f',
		});
		container.add(box2);
		var box3 = Ti.UI.createView({
		    width: '33%',
		    height: '120dip',
		    backgroundColor: '#4f4',
		});
		container.add(box3);
		var box4 = Ti.UI.createView({
		    width: '33%',
		    height: '120dip',
		    backgroundColor: '#ff4',
		});
		container.add(box4);
	}
})

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值