【问题标题】:TextView inside TableLayout and TableRow doesn't wrap textTableLayout 和 TableRow 中的 TextView 不换行
【发布时间】:2015-02-28 22:05:44
【问题描述】:

我的应用程序中有一个片段存在一个小问题,该片段显示了有关 Google 地方的详细信息。问题在于显示其他用户的评论。

如图所示,cmets 文本视图中的文本未完全显示。有没有办法让 textview 换行?

问题的图像: http://postimg.org/image/84frbxd23/

我正在从代码中动态添加 TableRow 和 TextView。我尝试了以下方法:

  1. 设置椭圆结束
  2. 将可滚动设置为 true
  3. 将 maxLines 设置为 10

这是我的布局:

<TableLayout
    android:id="@+id/reviews"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_below="@+id/openhours"
    android:layout_marginTop="20dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true">

</TableLayout>

这里是添加行和文本视图的代码:

        //Dinamically create table
        TableLayout table = (TableLayout)rootView.findViewById(R.id.reviews);
        TableRow tr_head = new TableRow(rootView.getContext());
        tr_head.setBackgroundColor(Color.GRAY);
        tr_head.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));


        TextView label_author = new TextView(rootView.getContext());
        label_author.setText("Author");
        label_author.setTextColor(Color.WHITE);
        label_author.setPadding(5, 5, 5, 5);
        tr_head.addView(label_author);// add the column to the table row here

        TextView label_comment = new TextView(rootView.getContext());
        label_comment.setText("Comment"); // set the text for the header
        label_comment.setTextColor(Color.WHITE); // set the color
        label_comment.setPadding(5, 5, 5, 5); // set the padding (if required)
        tr_head.addView(label_comment); // add the column to the table row here


        table.addView(tr_head, new TableLayout.LayoutParams(
                TableLayout.LayoutParams.FILL_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT));

        //if it has reviews display them
        if(currentP.hasReviews()) {
            for (Review r : currentP.getReviews()) {
                TableRow tr = new TableRow(rootView.getContext());
                tr.setLayoutParams(new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));

                //Create two columns to add as table data
                // Create a TextView to add date
                TextView author = new TextView(rootView.getContext());
                author.setText(r.getAuthor());
                author.setPadding(2, 0, 5, 0);
                author.setTextColor(Color.BLACK);
                author.setHorizontalScrollBarEnabled(false);
                tr.addView(author);

                TextView comment = new TextView(rootView.getContext());
                comment.setPadding(5, 0, 5, 0);
                comment.setText(r.getReview());
                comment.setTextColor(Color.BLACK);
                comment.setEllipsize(TextUtils.TruncateAt.END);
                comment.setMaxLines(10);
                comment.setId(r.getId());
                tr.addView(comment);


                // finally add this to the table row
                table.addView(tr, new TableLayout.LayoutParams(
                        TableLayout.LayoutParams.FILL_PARENT,
                        TableLayout.LayoutParams.WRAP_CONTENT));

            }

【问题讨论】:

    标签: java android layout textview android-tablelayout


    【解决方案1】:

    我会设置作者 TextView 常量的宽度,使其不会扩展太多并缩小 Comment TextView。对于 Comment TextView 设置给定的布局参数,以便它可以垂直扩展并且不会被截断。

                    // Create a TextView to add date
                    TextView author = new TextView(this);
                    author.setText(authorName);
                    author.setPadding(2, 0, 5, 0);
                    author.setTextColor(Color.BLACK);
                    author.setHorizontalScrollBarEnabled(false);
                    author.setWidth(150); // good idea to set width for author name so that its fixed
                    tr.addView(author);
    
                    TextView comment = new TextView(this);
                    comment.setPadding(5, 0, 5, 0);
                    comment.setText(bigComment);
                    comment.setTextColor(Color.BLACK);
                    comment.setMaxLines(10);
    
                   // Provide this parameter so that the whole text can be seen with no cutoff
                    comment.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,
                            TableRow.LayoutParams.WRAP_CONTENT,1.0f));
                        tr.addView(comment);
    

    【讨论】:

      【解决方案2】:

      尝试将 LayoutParams 设置为 label_comment。设置WRAP_CONTENT为宽度。

      另外,我建议您使用MATCH_PARENT 而不是FILL_PARENT,因为最后一个已弃用。

      【讨论】:

        【解决方案3】:

        您可能希望将layout_width 参数添加到您的 cmets 并将其设置为 wrap_content。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-14
          • 2011-02-10
          • 1970-01-01
          • 1970-01-01
          • 2012-05-17
          • 2012-04-22
          • 2014-01-30
          • 2014-10-01
          相关资源
          最近更新 更多