用ScrollView实现RadioGroup的滚动

来源:互联网 发布:php 围棋提子 编辑:程序博客网 时间:2024/06/07 12:05

    最近在制作一个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.

0 0
原创粉丝点击