简体   繁体   中英

Android: Dynamically added TextView not wrapping text

Dynamically adding TextViews to this layout results in text not being wrapped.

<!-- R.layout.description_card -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout_InfoShow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/table_row_padding_bottom" >

    <TableLayout 
        android:background="@drawable/card_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:id="@+id/TableRow_DescriptionTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/DescriptionTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="sans-serif-light"
                    android:textSize="@dimen/page_name_size"
                    android:text="Loading data..." />

        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/DescriptionTitle">

                <LinearLayout android:id="@+id/LinearLayout_Description"
                    android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

                        <!-- Dynamically added TextView here -->

                </LinearLayout>

        </TableRow>
    </TableLayout>

</LinearLayout>

I dynamically add the TextViews to this layout using these methods:

    View view = inflater.inflate(R.layout.description_card, null);
                        TextView title = (TextView) view.findViewById(R.id.DescriptionTitle);
                        LinearLayout description = (LinearLayout) view.findViewById(R.id.LinearLayout_Description);
ArrayList<TextView> textViews = des.getText();
            Iterator<TextView> iter = textViews.iterator();
                 while(iter.hasNext()){
                    TextView textView = iter.next();
                    description.addView(textView); 
                }

This results in my textviews not wrapping onto new lines, even though it works perfectly fine if I include the TextView directly in the XML file.

It results in this: 截图

Edit: TextView layout:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextViewDescription"
    style="@style/AppTheme"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingRight="8dp"
    android:text="Loading data..."
    android:textSize="@dimen/description_size"
    android:layout_weight="1"
    android:ellipsize="none"
    android:scrollHorizontally="false" />

You probably need to set it to multi line. Try

setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);

If you have other InputType attributes, you'll need to "or" them together.

At this part of your code, you are getting your TextView array:

ArrayList<TextView> textViews = des.getText();

And we don't see how those TextView 's are created. But I assume you did not set single_line or max_lines to TextView s.

Try this code while adding your TextView s to your LinearLayout:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

while (iter.hasNext()) {
    TextView textView = iter.next();
    description.addView(textView, params);
}

Edit: Use below layout while creating your TextView s(I've removed some attrs and style.):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TextViewDescription"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingRight="8dp"
    android:text="Loading data..."
    android:textSize="@dimen/description_size"/>

Edit2: Change your layout_width param to wrap_content . Also see updated param at code above. (This worked for me)

<LinearLayout android:id="@+id/LinearLayout_Description"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <!-- Dynamically added TextView here -->
</LinearLayout>

Try using something other than TableLayout. I'm not very experienced with TableLayout but I think it needs the child views to have relatively short and consistent widths.

Instead, try using LinearLayout, like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout_InfoShow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="@dimen/table_row_padding_bottom" >

    <TextView
        android:id="@+id/DescriptionTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:textSize="@dimen/page_name_size"
        android:text="Loading data..." />

    <!-- Dynamically added TextView here -->

</LinearLayout>

And add the views with more-or-less the same code you had before. Note I changed the parent view for supporting the adds -- outerLayout.

View view = inflater.inflate(R.layout.description_card, null);

TextView outerView = (TextView) view.findViewById(R.id.LinearLayout_InfoShow);
ArrayList<TextView> textViews = des.getText();

Iterator<TextView> iter = textViews.iterator();
while(iter.hasNext()) {
  TextView textView = iter.next();
  outerView.addView(textView); 
}

Found the reason.

The inflater needed a ViewGroup for the layout params to take effect. I had to change the way I pass the text through to the iterator but this is basically what needed changing:

TextView textView = (TextView) inflater.inflate(text.getViewID(), description, false);

For the problem... When you have

<TableLayout>
  <TableRow>
    <TextView>

    </TextView>
  </TableRow>
</TableLayout>

to make the text appear without truncation, add

android:layout_weight="1"

inside the textview

<TextView>

</TextView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM