English 中文(简体)
加入不同案文
原标题:Add different TextViews to ScrollView

I Have activity that get some data from the internet, and shows it to the screen. I m using scroll view cause it s long text, I also want different text style for a different data,so I use few textViews with a different style and to show it on the Activity screen, my problem is that scroll view can handle only one view, so how can I use scrolling to show different style of Text view, I tried to add LinearLayout to the scrollView and add all the textViews dynamically in code to this LinearLayout ,but I m getting exception - scroll view can host only one direct child.

守则如下:

/** 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>
最佳回答

如果你在XML中界定了,那么你不必在你的代码中设置一个新的<代码>LinearLayout,但你必须以这种方式检索现有的代码。

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

Otherwise you have to remove the LinearLayout in your XML and add all by code.

问题回答

问题在于在<代码>ScrollView上制造集装箱。 您不应在活动中创立,而应从xml中界定:

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签