如何在JS文件Titanium中使用app.tss类

时间:2017-02-25 13:16:20

标签: titanium appcelerator appcelerator-titanium

我的app.tss中有一个与相关联的样式。我想在按钮上应用该类。如果我在xml或tss文件中应用该类,它可以正常工作,但是当我通过JS文件动态创建按钮时,如何实现样式类。

  

这是我的app.tss

/*

This is your global styles file. Selectors and rules you define
here will be applied throughout your app. However, these rules
have the lowest priority of any style settings.

For more information, see the "Style Priorities" section of
http://docs.appcelerator.com/platform/latest/#!/guide/Alloy_Styles_and_Themes

For example, the following would apply to all labels, windows,
and text fields (depending on platform) in your app unless you
overrode the settings with other TSS, XML, or JS settings:

'Label[platform=android,windows]': {
    color: '#000' // all platforms except Android and Windows default to black
}

'Window': {
    backgroundColor: '#fff' // white background instead of default transparent or black
}

'TextField[platform=android]': {
    height: Ti.UI.SIZE
}

*/

".buttonCls":{
    width:"40dp",
    height : "20dp",
    backgroundColor : "#ff0000",
}
  

这是我的xml

<Alloy>
    <Window class="container">
        <Label id="label" onClick="doClick">Hello, World</Label>
    </Window>
</Alloy>
  

这是我的JS文件

function doClick(e) {
    alert($.label.text);
}

var vDate = Ti.UI.createButton({
    title : "hello world",
});

$.index.add(vDate);

$.index.open();

如何在我的按钮 vDate 上设置课程 buttonCls

1 个答案:

答案 0 :(得分:3)

要了解如何使用全局类,请参阅以下链接:

http://docs.appcelerator.com/platform/latest/#!/guide/Dynamic_Styles

要解决您的问题,您可以使用以下任一方法:

$.addClass(vDate, "buttonCls");

vDate.applyProperties($.createStyle({
    classes: ["buttonCls"]
}));