动态将TextView添加到此布局中会导致文本不被包裹。
这导致我的文本视图不会自动换行,即使如果我直接在XML文件中包含TextView,它也可以完美地工作。
结果如下:
编辑:
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>
我使用以下方法动态地向此布局添加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);
}
这导致我的文本视图不会自动换行,即使如果我直接在XML文件中包含TextView,它也可以完美地工作。
结果如下:
![screenshot](https://i.imgur.com/MafH09Z.jpg)
<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" />