如何在布局中有多个子项时向Android布局添加垂直滚动条?

deu*_*on0 4 android android-layout

我一直试图弄清楚如何确保屏幕可以垂直滚动,因为有很多单选按钮,它们不适合屏幕.我已经尝试了Stackoverflow上发布的大多数解决方案,但是我一直在收到错误,这是我上次尝试时的布局代码:

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

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

 <RadioGroup
        android:id="@+id/radioSharing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioSharingYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_sharing_yes"  />

    <RadioButton
        android:id="@+id/radioSharingNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_sharing_no" />

</RadioGroup>

 <RadioGroup
    android:id="@+id/radioInternet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioInternetYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_internet_yes"  />

    <RadioButton
        android:id="@+id/radioInternetNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_internet_no" />

</RadioGroup>

<RadioGroup
    android:id="@+id/radioMap"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMapYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_map_yes"  />

    <RadioButton
        android:id="@+id/radioMapNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_map_no" />

</RadioGroup>

    <RadioGroup
    android:id="@+id/radioCalling"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioCallingYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_calling_yes"  />

    <RadioButton
        android:id="@+id/radioCallingNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_calling_no" />

</RadioGroup>

<RadioGroup
    android:id="@+id/radioDatabase"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioDatabaseYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_database_yes"  />

    <RadioButton
        android:id="@+id/radioDatabaseNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_database_no" />

</RadioGroup>

<Button
    android:id="@+id/btnSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:text="@string/btn_display" />


</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:"ScrollView只能托管一个直接孩子"

任何建议将不胜感激,谢谢

Izk*_*ata 19

仔细看看你的筑巢:

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

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

 <RadioGroup
 ......
 </RadioGroup>

</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

你应该完全在ScrollView中使用LinearLayout,如下所示:

<?xml version="1.0" encoding="utf-8"?>

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

 <RadioGroup
 ......
 </RadioGroup>

</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)