2

I'm teaching myself the basics for NativeScript.

This is my main-page.xml:

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
  <GridLayout id="grid">

  </GridLayout>
</Page>

This is the JS code:

var view = require("ui/core/view");
var layout = require("ui/layouts/grid-layout");

function pageLoaded(args) {
    var page = args.object;
    var gr = view.getViewById(page, "grid");
    if (gr) {
        for (var i = 0; i < 10; i++) {
            gr.addColumn(new layout.ItemSpec(1, layout.GridUnitType.star));
            var g = new layout.GridLayout();
            g.style = "{ background-color: red; }";
            layout.GridLayout.setColumn(g, i);
            gr.addChild(g);
        }
    }
}

exports.pageLoaded = pageLoaded;

How does one programmatically set the style for a grid created in code-behind? When I run the code, I don't see anything in red. Ideally, I'd like to be able to add styles without creating a css class or selecting css by id. Is this possible i.e. adding in-line styles?

EDIT: Fixed the typo in "{ backgroud-color: red; }";

2
  • 'Background' is misspelled in your example. Are you that's not the problem? Also, do g.style.backgroundColor = "red"; Commented Jun 5, 2015 at 16:37
  • 1
    Miguel, I fixed the typo but that didn't work. I tried your second suggestion, and it seems to work. Thanks!! Commented Jun 5, 2015 at 16:43

1 Answer 1

3

You can set the background as a property of the style object:

g.style.backgroundColor = new colorModule.Color("Red");

Check the "JavaScript property" column here if you need to access other properties for the style object.

Sign up to request clarification or add additional context in comments.

1 Comment

i didnt find the .display = "block" or .display = "none" style in nativescript. so how do nativescript v8 styling used to make a view visible and hidden anyway? @Alexander

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.