【问题标题】:Add different TextViews to ScrollView向 ScrollView 添加不同的 TextView
【发布时间】:2012-01-14 14:39:04
【问题描述】:

我有从互联网获取一些数据并将其显示到屏幕上的活动。 我正在使用滚动视图,因为它是长文本,我还希望为不同的数据使用不同的文本样式,所以我使用了一些具有不同样式的 textView 并将其显示在 Activity 屏幕上, 我的问题是滚动视图只能处理一个视图,所以我如何使用滚动来显示不同样式的文本视图,我尝试将 LinearLayout 添加到 scrollView 并在代码中动态添加所有 textViews 到这个 LinearLayout ,但我出现异常 - 滚动视图只能承载一个直接子级。

以下代码:

/** this is the function, which called from the onClick method.
wanted data object contains 2 strings title message and the message itself.

When debug the code i can see that there's two String values in each loop.
but i cant add the linearLayout to my scrollView - exception ScrollView can host only one direct child */

    private void showResult(ArrayList<WantedData> result) {
        // TODO Auto-generated method stub
        TextView title;
        TextView data;
        scrollLayout = (LinearLayout) findViewById(R.id.LlScrollView);  
        for (WantedData curr : result) {
            if (curr.getTitle() == null) {
                break;
            }

            title = new TextView(this);
            title.setText(curr.getTitle());

            scrollLayout.addView(title, LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            data = new TextView(this);
            data.setText(curr.getData());   
            scrollLayout.addView(data, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        }
        scroll.addView(scrollLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        //at the onCreate method -  scroll = (ScrollView) findViewById(R.id.SvShowTextFromServer);
    }

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <include
        android:id="@+id/layout_reffernce"
        layout="@layout/explore" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Enter City" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <EditText
                android:id="@+id/EtCity"
                android:layout_width="210dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.14"
                android:orientation="vertical" >

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/bSearchCity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search" />
        </LinearLayout>

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Enter State" />

        <EditText
            android:id="@+id/EtState"
            android:layout_width="253dp"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/SvShowTextFromServer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/LlScrollView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/backround"
            android:orientation="vertical" >
        </LinearLayout>


    </ScrollView>

</LinearLayout>

【问题讨论】:

  • 请提供添加TextView的代码
  • scrollLayout = new LinearLayout(this); scroll.addView(scrollLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); for (WantedData curr : result) { if (curr.getTitle() == null) { break; } 标题 = 新的 TextView(this); title.setText(curr.getTitle()); scrollLayout.addView(title, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);数据 = 新的 TextView(this); data.setText(curr.getData()); scrollLayout.addView(data, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

标签: android textview scrollview


【解决方案1】:

问题是在ScrollView 中双重创建容器。您不应该在活动中创建它,而是从 xml 中获取已定义的内容:

LinearLayout scrollContainer = (LinearLayout)findViewById(R.id.LlScrollView);
for (...) {
    //create here some text
    scrollLayout.addView(text);
}

【讨论】:

  • 谢谢,我知道,但这不是问题,我已经删除了代码中的创建并添加了对 xml 文件的引用,仍然出现异常,滚动视图只能承载一个直接子项。 . 也许每个添加到 LinearLayout 的文本视图就像添加到滚动视图
【解决方案2】:

如果您在 XML 中定义了 LinearLayout,则不必在代码中创建新的 LinearLayout,但您必须以这种方式检索现有的 LinearLayout

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LlScrollView);

否则您必须删除 XML 中的 LinearLayout 并通过代码添加所有内容。

【讨论】:

  • 谢谢,我知道,但这不是问题,我已经删除了代码中的创建,但仍然得到异常滚动视图只能承载一个直接子视图。也许每个文本视图都添加到LinearLayout 就像添加到滚动视图中
  • @david:很奇怪,报告onCreate()方法的所有代码-编辑你的问题-
猜你喜欢
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-22
  • 2013-03-08
  • 1970-01-01
相关资源
最近更新 更多