Android 4.0 在GridLayout中模仿RadioButton单选按钮

这篇博客介绍了如何在Android 4.0及以上版本利用GridLayout模仿RadioButton的单选功能,特别是在平板开发中,创建美观的网格布局。作者通过实例展示了如何动态添加银行信息,并实现点击确定后显示选中银行的效果。关键点包括:将ImageView作为单选按钮,保存选中银行ID,以及利用tag保存和更新选中状态。
摘要由CSDN通过智能技术生成

Android中的RadioButton必须直接以RadioGroup为父组件才能发挥作用,而RadioGroup只能设置”横向”和”纵向”。

在pad开发中,因为屏幕比较开阔,因此,对于一些单项选择,其实做成GridView样式更美观。

在Android4.0以上的API中,提供了GridLayout这个布局,可以实现网格布局,以一个银行选择弹出框为例,记个小笔记。

 

效果如下:



点击左上角的其他银行,弹出AlertDialog,并动态的添加了银行数组内所有银行的信息。单选按钮其实是imageview,点击确定后,被选中的银行信息会显示在界面上,dialog也会推出,当下次再弹出dialog时,默认被选中的条目将是当前界面显示银行条目。

 

布局:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btn_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="其他银行" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/iv_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
   
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值