简体   繁体   English

Android:动态添加TextView而不包装文本

[英]Android: Dynamically added TextView not wrapping text

Dynamically adding TextViews to this layout results in text not being wrapped. 动态地将TextView添加到此布局会导致文本未被包装。

<!-- 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: 我使用以下方法动态地将TextView添加到此布局:

    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. 这导致我的文本视图不会换行到新行,即使它直接在XML文件中包含TextView也能正常工作。

It results in this: 结果如下: 截图

Edit: TextView layout: 编辑:TextView布局:

<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. 如果您有其他InputType属性,则需要将它们“或”放在一起。

At this part of your code, you are getting your TextView array: 在这部分代码中,您将获得TextView数组:

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

And we don't see how those TextView 's are created. 我们没有看到如何创建这些TextView But I assume you did not set single_line or max_lines to TextView s. 但我相信你并没有设置single_linemax_linesTextView秒。

Try this code while adding your TextView s to your LinearLayout: TextView添加到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使用下面的布局(我删除了一些attrs和样式。):

<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 . Edit2:layout_width参数更改为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. 尝试使用TableLayout以外的东西。 I'm not very experienced with TableLayout but I think it needs the child views to have relatively short and consistent widths. 我对TableLayout不是很熟悉,但我认为它需要子视图具有相对较短且一致的宽度。

Instead, try using LinearLayout, like this: 相反,尝试使用LinearLayout,如下所示:

<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. 注意我更改了父视图以支持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. inflater需要一个ViewGroup才能使布局参数生效。 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>

</TextView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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