简体   繁体   中英

Adding scrollview to relative layout messing up everything

I am a beginner in android development. My problem is that whenever i am applying scroll view to my Relativelayout everything is messing up. Can anyone please tell me how to apply scroll view to my app and one more problem when i am opening my app in portrait mode and when we will change it to landscape mode it will show application has stopped unexpectedly force close i guess it is may be due to because i haven't used scroll view in my application yet but still if anyone has any solution for it please tell me . Here is my code:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ccc"
    android:padding="30dp" >`enter code here`

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/hundred"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@drawable/tenshotrockets_back"
                android:text="      " />

            <Button
                android:id="@+id/ultimate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:background="@drawable/ultimaterockate_back"
                android:text="      " />

            <Button
                android:id="@+id/power"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="@drawable/powerrocket_back"
                android:text="      " />

            <Button
                android:id="@+id/two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:background="@drawable/twoshots_back"
                android:text="      " />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/hundred"
                android:text="hundred shots"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/two"
                android:text="two shots"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@id/textView3"
                android:layout_alignBottom="@id/textView3"
                android:layout_alignParentRight="true"
                android:text="ten shots"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/power"
                android:layout_below="@+id/power"
                android:text="power rocket"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/textView2"
                android:layout_alignBottom="@+id/textView2"
                android:layout_centerHorizontal="true"
                android:text="ultimate rocket"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/ten"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/music_blast"
                android:layout_alignBottom="@+id/music_blast"
                android:layout_alignLeft="@+id/textView8"
                android:background="@drawable/threeshots_back"
                android:text="      " />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/textView8"
                android:layout_alignBottom="@+id/textView8"
                android:layout_alignLeft="@+id/ultimate"
                android:text="musical blast"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/music_blast"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/textView5"
                android:layout_alignLeft="@+id/textView5"
                android:background="@drawable/musical_blast"
                android:text="      " />

            <Button
                android:id="@+id/music_rocket"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/Bottle_rock"
                android:layout_alignBottom="@+id/Bottle_rock"
                android:layout_alignRight="@+id/textView3"
                android:background="@drawable/musicalrocket_back"
                android:text="      " />

            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/music_rocket"
                android:layout_alignParentBottom="true"
                android:text="musical"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/Bottle_rock"
                android:layout_alignParentBottom="true"
                android:text="bottle rocket"
                android:textColor="#ffffff" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignRight="@+id/music_bank"
                android:text="sound"
                android:textColor="#ffffff" />

            <Button
                android:id="@+id/Bottle_rock"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/textView6"
                android:layout_alignLeft="@+id/textView5"
                android:background="@drawable/musicalsimple_back"
                android:text="      " />

            <Button
                android:id="@+id/music_bank"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/Bottle_rock"
                android:layout_alignBottom="@+id/Bottle_rock"
                android:layout_alignLeft="@+id/textView8"
                android:background="@drawable/musicalsmall"
                android:text="      " />
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

You need the fillViewport XML attribute in your ScrollView.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dp" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" > <!-- add this attribute -->

        <RelativeLayout ...

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

As for the app crashing when the orientation changes, take note that by default, the application restarts the activity (calls onCreate again) whenever there is an orientation change.

I'm guessing that probably messes up your program flow, but since you didn't post code, we can't really comment on it.

Perhaps you want to take a look at the multiple tutorials available.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM