Back

titanium 入门 1

发布时间: 2014-12-24 09:03:00

搭建环境不说了。

$ titanium create --name happy_soft --id=cc.happysoft --platforms=android,ios

就会生成一个文件夹。

然后,我们要使用alloy framework: 

$ cd happy_soft && alloy new

就会发现,目录下多了一个 app目录。

把手机插上,然后  $ adb devices 可以看到你的手机,就可以啦!

$ titanium build --target device --platform android

就会发现你的手机能跑起来了 。是不是能看到hello world了?

接下来,需要熟悉alloy: 

alloy 是针对MVC进行架构的。 目录分别是app目录下的 model, view, controller,  (下面代码不完整,只是局部代码)

controller:    ( app/controllers/index.js ) 

  1 function clickImage(e){
2 Titanium.UI.createAlertDialog({title: 'Image View', message: 'You clicked me!'}).show();
3 $.my_button.setTitle('Dashi ni hao!');
4 console.info(' creating button, dynamically');
5 }
6
7 $.index.open();
8

view:   ( app/views/index.xml ) 

 
  1 <Alloy>
2 <Window >
3 <ImageView id='imageView' onClick='clickImage' />
4 <Label id='l'>Click Image of Apple Logo </Label>
5 <Button id='my_button'>hi Siwei</Button>
6 </Window>
7 </Alloy>

还可以针对某个xml进行样式设置:

  1 ".container": {
2 backgroundColor:"white"
3 }
4
5 "Label": {
6 width: Ti.UI.SIZE,
7 height: Ti.UI.SIZE,
8 color: "#000"
9 }
10
11 "#label": {
12 font: {
13 fontSize: 12
14 }
15 }

还可以对不同的平台进行不同的配置:

Back