android - 如何以编程方式将 TextView 添加到具有线性布局和现有 TextView 的 ScrollView

标签 android scrollview

我有以下内容: - 带有字符串的 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/

相关文章:

android - 无法使用 PreferenceActivity 设置首选项

android - 使用自定义 View android在文本和下划线之间留出空间

Android:使用背景png图像

android - 如何在普通 ScrollView 中滚动拉动刷新 ScrollView

react-native - TouchableOpacity 作为 ListView 中的 Item 仅在 TextInput 失去焦点后才会 react

android - webview 和 scrollview 的问题

java - 如何让应用程序的启动图标运行首选项 Activity ?

android - 在android中合并适配器

c# - Windows 10 通用应用程序中的滚动查看器有问题吗?

单击edittext时android自动滚动