Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

java - A scroll view can have only one child Android XML file

So, I want to have a scrollview on my layout. I did this and got a scroll view can only have one child :

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

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="95dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:text="Latitude: "
        android:textSize="15dip" >

    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="unknown"
        android:textSize="15dip" >

    </TextView>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="90dip"
        android:layout_marginRight="5dip"
        android:text="Longitute: "
        android:textSize="15dip" >
    </TextView>

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unknown"
        android:textSize="15dip" >
    </TextView>
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="130dip"
    android:layout_height="30dip"
    android:layout_marginLeft="95dip"
    android:layout_marginTop="10dip"
    android:text="Find Coordinates"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextName"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="20dip"
    android:ems="10"
    android:hint="Enter Location Name"
    android:textSize="10dip" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editTextPhone"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="0dip"
    android:ems="10"
    android:hint="Enter Phone Number"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextEmail"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Email"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextDesc"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Description"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFlowers"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Flowers"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextPlants"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Plants"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFruit"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Fruit"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextVeggies"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Veggies"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextSun"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="115dip"
    android:ems="10"
    android:hint="Sunday"
    android:textSize="10dip" />

<Button
    android:id="@+id/saveLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="90dip"
    android:layout_marginTop="10dip"
    android:text="Save Location" />

<Button
    android:id="@+id/seeAll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="120dip"
    android:text="See all" />

</ScrollView> 

Then, following the answer to this question

java.lang.IllegalStateException: ScrollView can host only one direct child

I did this:

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

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="95dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:text="Latitude: "
        android:textSize="15dip" >

    </TextView>

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="unknown"
        android:textSize="15dip" >

    </TextView>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation = "vertical" >

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="90dip"
        android:layout_marginRight="5dip"
        android:text="Longitute: "
        android:textSize="15dip" >
    </TextView>

    <TextView
        android:id="@+id/TextView04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="unknown"
        android:textSize="15dip" >
    </TextView>
</LinearLayout>

<Button
    android:id="@+id/button1"
    android:layout_width="130dip"
    android:layout_height="30dip"
    android:layout_marginLeft="95dip"
    android:layout_marginTop="10dip"
    android:text="Find Coordinates"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextName"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="20dip"
    android:ems="10"
    android:hint="Enter Location Name"
    android:textSize="10dip" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/editTextPhone"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:layout_marginTop="0dip"
    android:ems="10"
    android:hint="Enter Phone Number"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextEmail"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Email"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextDesc"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="25dip"
    android:ems="10"
    android:hint="Enter Description"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFlowers"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Flowers"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextPlants"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Plants"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextFruit"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dip"
    android:hint="Fruit"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextVeggies"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="180dip"
    android:layout_marginTop="-35dip"
    android:hint="Veggies"
    android:ems="10"
    android:textSize="10dip" />

<EditText
    android:id="@+id/editTextSun"
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="115dip"
    android:ems="10"
    android:hint="Sunday"
    android:textSize="10dip" />

<Button
    android:id="@+id/saveLocation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="90dip"
    android:layout_marginTop="10dip"
    android:text="Save Location" />

<Button
    android:id="@+id/seeAll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="120dip"
    android:text="See all" />

</ScrollView> 

but still no luck. Any ideas? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The key here is

ScrollView can host only one direct child

"direct" being the keyword. So you can have

<ScrollView
    ...>
    <LinearLayout>
       ....
       <!-- you can have as many Views in here as you want -->
    </LinearLayout>
</ScrollView>

So, the LinearLayout in the above example would be your one direct child. And that child can have as many children as you want. Which means you can have all of your other LinearLayouts inside that first one. But you can't have more than one on the same hierarchical level.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...