用一行TextViews以编程方式填充LinearLayout

时间:2013-02-05 16:04:30

标签: android textview width android-linearlayout

我试图通过将TextView放在单行LinearLayouts行中来在屏幕上显示一堆文本。每个单词都存储在它自己独立的TextView中,我希望能够放置尽可能多的TextViews,以适应单个LinearLayout行,并检测我何时用尽水平空间,以便我可以移动到下一行。

我面临的问题是,在创建显示时,我似乎无法找到一种方法来测量更改的布局大小,因为我无法在父级上使用getWidth()获取参考宽度布局,甚至在我添加TextViews后,我似乎无法控制宽度。

之前我们有一个工作版本,但它根据TextView中固定大小的字符数使用硬编码数字完成所有操作。我正在尝试扩展应用程序以使用所有文本和屏幕大小。如果这需要彻底检修,我理解 - 我只是希望能够用无限数量的文本行填满屏幕。

一个明显的解决方案是将所有文本放在一个TextView中,但我们需要能够通过显示的TextViews访问每个Word / Ponctuation对象及其属性。

// layout_row is the current LinearLayout row I'm adding my TextViews to
// layout is the LinearLayout parent of all layout_rows
// text.text_content is a linked list of Word and Ponctuation objects
// each Word and Ponctuation object has a TextView attribute called view

private void display_views() {

    if (text != null)
    {
        boolean prochainLigneSuivante; // if new line is to follow
        int widthSoFar = 0;
        int layoutWidth = layout_row.getWidth();  

        for (Object o : text.text_content) {
            if (o instanceof Word ) {
                Word w = (Word) o;
                Object next = text.next(o);

                if  (noNeedForSpace(w)) {
                    // by default all TextViews have 
                    // right padding to simulate spaces
                    w.view.setPadding(0, 0, 0, 0);
                }

                layout_row.addView(w.view);
                widthSoFar += w.view.getWidth();

                // Am I 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);

}

0 个答案:

没有答案