Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式使用单行文本视图填充LinearLayout_Android_Textview_Width_Android Linearlayout - Fatal编程技术网

Android 以编程方式使用单行文本视图填充LinearLayout

Android 以编程方式使用单行文本视图填充LinearLayout,android,textview,width,android-linearlayout,Android,Textview,Width,Android Linearlayout,我试图通过将文本视图放在单行线性布局的行中,在屏幕上显示一组文本。每个单词都存储在自己单独的文本视图中,我希望能够将尽可能多的文本视图放置在一条LinearLayout行上,并在水平空间用完时进行检测,以便移动到下一行 我面临的问题是,在创建显示时,我似乎找不到一种方法来测量不断变化的布局大小,因为我无法在父布局上使用getWidth()获得参考宽度,即使在添加TextView之后,我似乎也无法控制宽度 我们以前有一个工作版本,但它使用基于文本视图中固定大小的字符数的硬编码数字。我正在尝试扩展该

我试图通过将文本视图放在单行线性布局的行中,在屏幕上显示一组文本。每个单词都存储在自己单独的文本视图中,我希望能够将尽可能多的文本视图放置在一条LinearLayout行上,并在水平空间用完时进行检测,以便移动到下一行

我面临的问题是,在创建显示时,我似乎找不到一种方法来测量不断变化的布局大小,因为我无法在父布局上使用
getWidth()
获得参考宽度,即使在添加TextView之后,我似乎也无法控制宽度

我们以前有一个工作版本,但它使用基于文本视图中固定大小的字符数的硬编码数字。我正在尝试扩展该应用程序,使其能够处理所有文本和屏幕大小。如果这需要彻底检修,我理解-我只想能够用不确定数量的文本行填充屏幕

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

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

}

为每个单词创建文本视图效率很低,是否有特定的需要?如果是用于文本格式,则可以使用
Html.fromHtml
获取基本的Html样式(粗体、斜体等)。不幸的是,需要将每个单词放在单独的文本视图中。我们需要能够分别更改每个单词的背景和颜色,以及访问单词/主题对象的属性。在单个文本视图中使用SpannableString是否可能?您当然可以使用
SpannableString
更改背景和文本颜色。不过,要获得与跨度相关联的Word/Pontuation对象,您可能可以使用SparseArray,将单词位置作为索引?谢谢-我现在正在处理这个问题。如果我继续这个想法,你知道在TextView中是否可以确定子字符串的屏幕位置,也许给定它的索引?(到目前为止我还没有发现任何东西。)这就是为什么我们有单独的行布局作为文本的“行”,因为我们希望能够将屏幕滚动到我们可能关注的任何单词。实际上,记住这个想法——找到这个就是为了找到一个SpannableString