简体   繁体   English

将滚动视图添加到相对布局使所有事情搞砸了

[英]Adding scrollview to relative layout messing up everything

I am a beginner in android development. 我是android开发的初学者。 My problem is that whenever i am applying scroll view to my Relativelayout everything is messing up. 我的问题是,每当我将滚动视图应用于我的Relativelayout一切都会混乱。 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. 您需要在ScrollView中使用fillViewport XML属性。

<?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. 至于方向更改时应用程序崩溃,请注意,默认情况下,方向更改时,应用程序将重新启动活动(再次调用onCreate )。

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. 也许您想看一下可用的多个教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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