![](/img/trans.png)
[英]Android how to add Textview Programatically line by line in linearlayout
[英]How to add TextView's programatically to a ScrollView with a LinearLayout and existing TextViews
我有以下內容:-帶有字符串的映射,如:[“ Color:blue”,“ Size:big”],稱為detailsArray
具有LinearLayout和現有TextView的現有ScrollView:
<ScrollView>
<LinearLayout>
<TextView
android:text="something: else"
/>
</LinearLayout>
</ScrollView>
我故意省略了諸如寬度,高度,xml模式之類的通用字段。
現在,我想以編程方式添加textViews。 我不知道他們有很多:
TextView detail;
LinearLayout llay = (LinearLayout)fragmentView.findViewById(R.id.container);
for (int i = 0; i < detailsArray.length; i++) {
detail = new TextView(fragmentView.getContext());
detail.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
detail.setText(detailsArray[i]);
llay.addView(detail);
}
ScrollView sv = (ScrollView) fragmentView.findViewById(R.id.scroll_view);
sv.addView(llay);
但是我遇到一個例外:
04-17 12:38:09.975: E/AndroidRuntime(3361): Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child
我該怎么辦? 先感謝您。
您應該刪除sv.addView(llay);
因為您基本上是將線性布局兩次添加到同一個ScrollView中 ,所以您將收到異常,以及為什么在開始時刪除AllViews可以解決此問題。 在完成for循環調用之后
sv.invalidate();
sv.requestLayout();
應該使它刷新其內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.