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

时间:2012-05-15 20:18:03

标签: 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>

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

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

2 个答案:

答案 0 :(得分: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>

您应该完全在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>

答案 1 :(得分:0)

第一个答案有一个错误。

代替

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

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

使用

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