用ScrollView实现RadioGroup的滚动

当RadioGroup包含大量RadioButton超出屏幕时,将Linearlayout包裹在ScrollView内可实现滚动显示。ScrollView作为 Framelayout,通常放一个包含全部内容的子视图,如垂直方向的LinearLayout。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    最近在制作一个app,其中一个活动使用了RadioGroup控件,该控件包含了几十个RadioButton供用户选择,这么多的RadioButton无法显示在一个页面内,大多数超出了页面的范围,需要滚动显示。刚开始时,只是把RadioGroup控件放在了linearlayout布局内,但是这种情况下无法滚动显示。后来我把linearlayout放在一个ScrollView控件内,所有的RadioButton就可以滚动显示了。如下所示:

 

 
 
<?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 :fillViewport="false">
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android :orientation= "vertical"
    android :layout_width="match_parent"
     android :layout_height="match_parent" >
    < RadioGroup
        android :id= "@+id/radioGroup1"
        android :layout_width="wrap_content"
        android :layout_height="wrap_content"
        android :layout_alignParentLeft="true"
        android :layout_alignParentRight="true"
        android :layout_alignParentTop="true"
        android :layout_marginLeft="80dp">
        < RadioButton
        android :id= "@+id/radio1"
        android :layout_width="wrap_content"
        android :layout_height="wrap_content"
            android :layout_marginBottom="15dp"
        android :checked="true"
        android :text="1" />
        < RadioButton
            android :id= "@+id/radio2"
            android :layout_width="wrap_content"
            android :layout_height="wrap_content"
            android :layout_marginBottom="15dp"
            android :text="2" />
        < RadioButton
            android :id= "@+id/radio3"
            android :layout_width="wrap_content"
            android :layout_height="wrap_content"
            android :layout_marginBottom="15dp"
            android :text="3" />
         …
 
 
</RadioGroup>
</ LinearLayout>
</ ScrollView>

 

    事实上,ScrollView是一个FrameLayout,因此,它里面只能放一个子女,最经常放的就是一个linearlayout。把需要滚动显示的内容放到该linearlayout中即可。

    可以参考下面关于ScrollView的基本描述:

    Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值