android - Programmatically fill LinearLayout with a single line of TextViews -
i'm trying display bunch of text on screen placing textviews within rows of single-line linearlayouts. each word stored in own separate textview, , want able place many textviews fit on single linearlayout line , observe when i've run out of horizontal space can move next line.
the problem i'm facing can't seem find way measure changing layout sizes create display, because can't reference width using getwidth()
on parent layout, , after add together textviews, can't seem command width.
we had working version before, did using on hard-coded numbers based on number of characters in textview @ fixed size. i'm trying extend app work text , screen sizes. if needs overhauled, understand - want able fill screen indefinite number of lines of text.
an obvious solution place text within 1 textview, need able access each word/ponctuation object , attributes through displayed textviews.
// layout_row current linearlayout row i'm adding textviews // layout linearlayout parent of layout_rows // text.text_content linked list of word , ponctuation objects // each word , ponctuation object has textview attribute called view private void display_views() { if (text != null) { boolean prochainlignesuivante; // if new line follow int widthsofar = 0; int layoutwidth = layout_row.getwidth(); (object o : text.text_content) { if (o instanceof word ) { word w = (word) o; object next = text.next(o); if (noneedforspace(w)) { // default textviews have // right padding simulate spaces w.view.setpadding(0, 0, 0, 0); } layout_row.addview(w.view); widthsofar += w.view.getwidth(); // out of space? prochainlignesuivante = widthsofar >= layoutwidth; if(prochainlignesuivante) { layout_row.removeview(w.view); widthsofar = 0; layout_row = new linearlayout(context); layout_row.setorientation(linearlayout.horizontal); layout_row.addview(w.view); layout_row.setbackgroundcolor(color.black); layout_row.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.match_parent)); layout.addview(layout_row); } } else if (o instanceof ponctuation) { ponctuation p = (ponctuation) o; if (p.text.contains("cr")) { layout_row = new linearlayout(context); layout_row.setorientation(linearlayout.horizontal); layout_row.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.match_parent)); widthsofar = 0; layout.addview(layout_row); } else { if (p.view.gettext().equals(" ")) p.view.setpadding(0, 0, 0, 0); layout_row.addview(p.view); if(!p.view.gettext().equals("")) widthsofar += p.view.getwidth(); } } } } else { log.e("text", "text est nul"); } scroll.refreshdrawablestate(); transition.starttransition(0); }
android textview width android-linearlayout
No comments:
Post a Comment