当布局中有很多孩子时,如何向Android布局添加垂直滚动条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10608234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:20:41  来源:igfitidea点击:

How to add a vertical scroll bar to Android layout when there many children in the layout?

androidandroid-layout

提问by deucalion0

I have been trying to figure out how to ensure the screen can be scrolled vertically as there are many radio buttons and they don't fit on the screen. I have tried most of the solutions posted on Stackoverflow, but I keep getting errors, here is my layout code at my last attempt:

我一直在试图弄清楚如何确保屏幕可以垂直滚动,因为有很多单选按钮并且它们不适合屏幕。我已经尝试了 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>

The error I am getting is : "ScrollView can host only one direct child"

我得到的错误是:“ScrollView 只能承载一个直系子节点”

Any advice would be appreciated, Thanks

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

回答by Izkata

Take a closer look at your nesting:

仔细看看你的嵌套:

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

You should have LinearLayout entirely within ScrollView, like so:

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

回答by Victor Pozdnyakov

The first answer has got one mistake.

第一个答案有一个错误。

instead

反而

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

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

use

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

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


 ......
  </ScrollView>