TextView внутри TableLayout и TableRow не переносит текст

У меня есть небольшая проблема с фрагментом в моем приложении, который показывает подробности о Google Place. Проблема в отображении отзывов других пользователей.

Как показано на этом изображении, текст в текстовом представлении комментариев отображается не полностью. Есть ли способ заставить 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));

            }

person Died22    schedule 02.01.2015    source источник


Ответы (3)


Я бы установил константу ширины автора TextView, чтобы она не слишком расширялась и не уменьшала 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);
person ZakiMak    schedule 02.01.2015

Попробуйте установить для LayoutParams значение label_comment. Установите WRAP_CONTENT в качестве ширины.

Кроме того, я рекомендую вам использовать MATCH_PARENT вместо FILL_PARENT, так как последний устарел.

person Olaia    schedule 02.01.2015

Вы можете добавить к своим комментариям параметр layout_width и установить для них значение wrap_content.

person SalmonKiller    schedule 02.01.2015