![](/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.