我有以下内容: - 带有字符串的 map ,如:["Color: blue","Size: big"] 称为 detailsArray
带有 LinearLayout 和现有 TextView 的现有 ScrollView:
<ScrollView>
<LinearLayout>
<TextView
android:text="something: else"
/>
</LinearLayout>
</ScrollView>
我故意省略了宽度、高度、xml 模式等常见字段。
现在我想以编程方式添加 textView。 我不知道他们有多少:
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);
但是我遇到了一个异常(exception):
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 - 你得到的异常以及为什么什么时候你在一开始就删除了所有 View ,它解决了这个问题。
完成for循环调用后
sv.invalidate();
sv.requestLayout();
应该让它刷新它的内容。
关于android - 如何以编程方式将 TextView 添加到具有线性布局和现有 TextView 的 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064289/