Android RadioButton+GridLayout实现多行多列的单选效果

记录下实现过程,因为最近项目里要用到。我们都知道默认的RadioGroup+RadioButton是不能实现轻松换行的。如果每行使用一个RadioGroup来包裹RadioButton的话。其中的选择监听是个非常麻烦的事情。那么今天记录下RadioButton+GridLayout。

首先xml布局中添加一个GridLayout控件


<GridLayout
        android:id="@+id/radioGridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="3"
        android:rowCount="2"
        android:padding="16dp"
        android:alignmentMode="alignMargins"
        android:columnOrderPreserved="false"
        android:rowOrderPreserved="false">
    </GridLayout>

接着在activity中,添加下面的代码就可以了。要几个就加几个,不一定单行得满,比如3+2个也可以,3+3个也可以。

private List<RadioButton> radioButtons = new ArrayList<>();//RadioButton集合
protected void onCreate(@Nullable Bundle savedInsta

相关推荐

  1. Android RadioButton+GridLayout实现效果

    2024-06-11 01:24:02       32 阅读
  2. 基于目摄像头实现AR人脸捕捉效果展示

    2024-06-11 01:24:02       22 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 01:24:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 01:24:02       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 01:24:02       82 阅读
  4. Python语言-面向对象

    2024-06-11 01:24:02       91 阅读

热门阅读

  1. 使用Matplotlib进行Python绘图

    2024-06-11 01:24:02       29 阅读
  2. 构建SOA架构时应该注意的问题

    2024-06-11 01:24:02       28 阅读
  3. WPF Frame应用 实现页面跳转

    2024-06-11 01:24:02       31 阅读
  4. 中介子方程十一

    2024-06-11 01:24:02       33 阅读
  5. 对象创建的4种模式

    2024-06-11 01:24:02       25 阅读
  6. centos中sudo: apt: command not found

    2024-06-11 01:24:02       31 阅读