簡體   English   中英

將滾動視圖添加到相對布局使所有事情搞砸了

[英]Adding scrollview to relative layout messing up everything

我是android開發的初學者。 我的問題是,每當我將滾動視圖應用於我的Relativelayout一切都會混亂。 有人可以告訴我如何在我的應用程序中以縱向模式打開應用程序時將滾動視圖應用於我的應用程序以及另一個問題,當我們將其更改為橫向模式時,它將顯示應用程序已意外停止並強制關閉,我想這可能是由於我還沒有在應用程序中使用滾動視圖,但是如果有人有任何解決方案,請告訴我。 這是我的代碼:

    <?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>

您需要在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>

至於方向更改時應用程序崩潰,請注意,默認情況下,方向更改時,應用程序將重新啟動活動(再次調用onCreate )。

我猜想這可能會弄亂您的程序流程,但是由於您沒有發布代碼,因此我們無法對此發表評論。

也許您想看一下可用的多個教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM