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

How to use app.tss classes in JS file Titanium

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

This is my 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",
}

This is my xml

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

This is my JS file

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

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

$.index.add(vDate);

$.index.open();

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

要了解如何使用全局 类,请参阅此 link:

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

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

$.addClass(vDate, "buttonCls");

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