![](/img/trans.png)
[英]Android: Programmatically added Textview not wrapping text to next line
[英]Android: Dynamically added TextView not wrapping text
动态地将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也能正常工作。
结果如下:
编辑: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" />
您可能需要将其设置为多行。 尝试
setInputType (InputType.TYPE_TEXT_FLAG_MULTI_LINE);
如果您有其他InputType属性,则需要将它们“或”放在一起。
在这部分代码中,您将获得TextView
数组:
ArrayList<TextView> textViews = des.getText();
我们没有看到如何创建这些TextView
。 但我相信你并没有设置single_line
或max_lines
到TextView
秒。
将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);
}
编辑:在创建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:将layout_width
参数更改为wrap_content
。 另请参阅上面代码中的更新参数。 (这对我有用)
<LinearLayout android:id="@+id/LinearLayout_Description"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Dynamically added TextView here -->
</LinearLayout>
尝试使用TableLayout以外的东西。 我对TableLayout不是很熟悉,但我认为它需要子视图具有相对较短且一致的宽度。
相反,尝试使用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>
并使用您之前使用的相同代码添加视图。 注意我更改了父视图以支持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);
}
找到原因。
inflater需要一个ViewGroup才能使布局参数生效。 我不得不改变将文本传递给迭代器的方式,但这基本上是需要改变的:
TextView textView = (TextView) inflater.inflate(text.getViewID(), description, false);
对于这个问题...当你有
<TableLayout>
<TableRow>
<TextView>
</TextView>
</TableRow>
</TableLayout>
要使文本显示而不截断,请添加
android:layout_weight="1"
在textview中
<TextView>
</TextView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.