Manuel_Rodrigues Manuel_Rodrigues - 6 months ago 19
Android Question

Place a LabelView dynamically under another LabelView

I have two

LabelViews
. The text added to the views is of variable size. how can I put the second
LabelView
immediately below the first
LabelView
without knowing the height of it?

How can I get the height of the first
LabelView
if it is set as 'automatic' to fit the text?

PS: I'm not using Alloy.

Answer

Well, @Manuel_Rodrigues I don't understand clear of your problem! If you have this problem in Titanium, the follow suggestion may help you. First, the LabelViews you said is the UI component of Label or View? And, according your descriptions I'm wonder are you want to do something like CHAT IN TEXT that the follow content of the chat under the last people chat content. If this is what you want, then:

1.Make the text auto fit the label

var label = Ti.UI.createLabel({
    font:{
        fontSize: '18'                          //text font size
    },
    top: 10,                                    //the distance between the labels
    width: 'auto',                              //automatic define width by the text
    height: 'auto',                             //automatic define height by the text
    textAlign: 'center',                        //text display in the center of the label in horizontal direction
    verticalAlign: Ti.UI.TEXT_ALIGNMENT_CENTER  //text display in the center of the label in vertical direction
});

please note this:

If you set the width and height both with value auto, when the text is very long that the mobile screen can't display all the content, the label will not make a new line to display the rest text. So, the better way is set a specified value to the width or equal to the width of the mobile screen.

2.The next label display under the last label automatically

If you choose the View as the parent of these lables, you should set the property layout with value vertical. Well, if there's only window you defined, do the same thing.

var view = Ti.UI.createView({
    width: '100%',
    height: 'auto',
    layout: 'vertical'
});
view.add(label1);
view.add(label2);
...

or for window:

var win = Ti.UI.createWindow({
    width: '100%',
    height: '100%',
    layout: 'vertical'
});
win.add(label1);
win.add(label2);
...

I hope this can help you!