Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向ScrollView添加不同的文本视图_Android_Textview_Scrollview - Fatal编程技术网

Android 向ScrollView添加不同的文本视图

Android 向ScrollView添加不同的文本视图,android,textview,scrollview,Android,Textview,Scrollview,我的活动是从互联网上获取一些数据,并将其显示在屏幕上。 我使用滚动视图是因为它是长文本,我也希望不同的数据有不同的文本样式,所以我使用了一些不同样式的文本视图,并将其显示在活动屏幕上, 我的问题是滚动视图只能处理一个视图,因此如何使用滚动来显示不同样式的文本视图,我尝试将LinearLayout添加到滚动视图中,并在代码中将所有的文本视图动态添加到该LinearLayout中,但我遇到了一个例外-滚动视图只能承载一个直接子视图 代码如下: /** this is the function, wh

我的活动是从互联网上获取一些数据,并将其显示在屏幕上。 我使用滚动视图是因为它是长文本,我也希望不同的数据有不同的文本样式,所以我使用了一些不同样式的文本视图,并将其显示在活动屏幕上, 我的问题是滚动视图只能处理一个视图,因此如何使用滚动来显示不同样式的文本视图,我尝试将LinearLayout添加到滚动视图中,并在代码中将所有的文本视图动态添加到该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);
    }
/**这是从onClick方法调用的函数。
想要的数据对象包含标题消息和消息本身两个字符串。
调试代码时,我可以看到每个循环中有两个字符串值。
但是我不能将linearLayout添加到我的scrollView-例外scrollView只能承载一个直接子级*/
私有无效显示结果(ArrayList结果){
//TODO自动生成的方法存根
文本视图标题;
文本视图数据;
scrollLayout=(LinearLayout)findViewById(R.id.LlScrollView);
用于(WantedData当前:结果){
if(curr.getTitle()==null){
打破
}
title=新文本视图(此);
title.setText(curr.getTitle());
scrollLayout.addView(标题、LayoutParams.FILL\u父项、,
LayoutParams.WRAP_内容);
数据=新文本视图(此);
data.setText(curr.getData());
scrollLayout.addView(数据、LayoutParams.FILL\u父项、LayoutParams.WRAP\u内容);
}
scroll.addView(scrollLayout、LayoutParams.FILL\u父级、LayoutParams.WRAP\u内容);
//在onCreate方法中-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>

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

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

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

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

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

否则,您必须删除XML中的
线性布局
,并按代码添加所有内容。

问题是在
滚动视图中重复创建容器。您不应在“活动”中创建它,而应使用已从xml定义的:

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

问题是在
ScrollView
中重复创建容器。您不应在“活动”中创建它,而应使用已从xml定义的:

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

提供添加代码
TextView
pleseScrollLayout=new LinearLayout(此);scroll.addView(scrollLayout、LayoutParams.FILL\u父级、LayoutParams.WRAP\u内容);对于(WantedData curr:result){if(curr.getTitle()==null){break;}title=new TextView(this);title.setText(curr.getTitle());scrollLayout.addView(title,LayoutParams.FILL\u PARENT,LayoutParams.WRAP\u CONTENT);data=new TextView(this);data.setText(curr.getData());scrollLayout.addView(数据,LayoutParams.FILL\u父项,LayoutParams.WRAP\u内容);提供添加代码
TextView
PleaseSrollLayout=new LinearLayout(此);scroll.addView(scrollLayout,LayoutParams.FILL\u父项,LayoutParams.WRAP\u内容);for(WantedData curr:result){if(curr.getTitle()=null){break;}title=new TextView(this);title.setText(curr.getTitle());scrollLayout.addView(title,LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容);data=new TextView(this);data.setText(curr.getData());scrollLayout.addView(data,LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容);谢谢,我知道,但恐怕这不是问题所在,我已经删除了代码中的创建内容,但仍然得到异常滚动视图只能承载一个直接子项..也许添加到LinearLayout的每个文本视图就像添加到滚动视图一样view@david:非常奇怪,报告
onCreate()的所有代码
method-编辑您的问题-谢谢,我知道,但这不是问题所在,我已经在代码处删除了创建内容,但仍然得到异常滚动视图只能承载一个直接子视图。可能添加到线性布局的每个文本视图都像添加到滚动视图一样view@david:非常奇怪,报告
onCreate()的所有代码
method-编辑您的问题-谢谢,我知道,但恐怕这不是问题所在,我已经删除了代码中的创建内容,并添加了对xml文件的引用,仍然会出现异常,滚动视图只能承载一个直接子视图。也许添加到LinearLayout的每个文本视图就像添加到滚动视图中一样谢谢,我知道,但这不是pr问题恐怕是,我已经删除了代码中的创建内容,并添加了对xml文件的引用,但仍然存在异常,滚动视图只能承载一个直接子视图。可能添加到LinearLayout的每个文本视图都类似于添加到滚动视图