android - 将 ScrollView 添加到相对布局搞砸了一切

标签 android android-layout

我是 android 开发的初学者。我的问题是,每当我将 ScrollView 应用于我的 Relativelayout 时,一切都搞砸了。任何人都可以告诉我如何将 ScrollView 应用于我的应用程序以及当我以纵向模式打开我的应用程序时的另一个问题,当我们将其更改为横向模式时它会显示应用程序意外停止强制关闭我想这可能是由于我还没有在我的应用程序中使用 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: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>

至于方向改变时应用程序崩溃,请注意,默认情况下,只要方向发生改变,应用程序就会重新启动 Activity(再次调用 onCreate)。

我猜这可能会打乱您的程序流程,但由于您没有发布代码,我们无法真正对此发表评论。

也许您想查看可用的多个教程。

关于android - 将 ScrollView 添加到相对布局搞砸了一切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19615006/

相关文章:

android - 用于检索数据的 Firebase 数据库引用

java - EditText 在 NestedScrollView 内不可滚动

android - 单击按钮后布局正在调整大小

android - Jetpack 撰写 : How to avoid Button Text disappearing when Button size is small?

android - 在 Titanium 中,如何让视频在 Android 中播放?

android - 如何使用 flutter 建立此屏幕的白色区域?

javascript - Android WebView 编译表单并使用 Javascript 提交

Android Location Wifi 重启后无法使用

java - 删除 BottomNavigationView 图标和中心标题

android - TabHost 动态添加标签页