Android RadioGroup 及资源文件 & selector
RadioGroup :单选组 RadioButton :单选按钮
RadioButton和CheckBox的区别:
- 1、单个RadioButton在选中后,通过点击无法变为未选中
- 单个CheckBox在选中后,通过点击可以变为未选中
- 一组RadioButton,只能同时选中一个
- 一组CheckBox,能同时选中多个
- RadioButton在大部分UI框架中默认都以圆形表示
- CheckBox在大部分UI框架中默认都以矩形表示
RadioButton和RadioGroup的关系:
- RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
- 每个RadioGroup中的RadioButton同时只能有一个被选中
- 不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
- 大部分场合下,一个RadioGroup中至少有2个RadioButton
- 大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置
在layout.xml图像界面中拖出一个RadioGroup(默认是纵向),会产生三个RadioButton

当我们把RadioGroup中的android:orientation 设为水平 horizontal

参照我们手机的App,是不是很多地方都是这样呢?那么现在我们去掉按钮

<?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" > <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:text="1" /> <RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="2" /> <RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="3" />
</RadioGroup> </LinearLayout>
=====================================================================================
<?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="horizontal" > <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="@drawable/group_buton_nomal"
android:gravity="center"> <RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/weixin"
style="@style/radioStyle"
android:drawableTop="@drawable/tab_weixin"/> <RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addressList"
style="@style/radioStyle"
android:drawableTop="@drawable/tab_address"/> <RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/find"
style="@style/radioStyle"
android:drawableTop="@drawable/tab_find"/> <RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set"
style="@style/radioStyle"
android:drawableTop="@drawable/tab_set"/>
</RadioGroup> </LinearLayout>
效果如图:

那么,很多人就不明白了。为什么我复制下来,一模一样敲出来怎么没有这效果,还报错。
首先,我们先来看一下里面引用的资源:
1. android:id ="@+id/radioGroup1"
- @+id 与@id 的区别
-->@+id 自动把起的ID名字加入到配置文件中,在业务逻辑中可以直接在R.id中找到,相当于创建。
-->@id在Value目录下写一个ids.xml来对每个id进行声明,相当与引用。
2. android:background ="@drawable/group_buton_nomal"
android:drawableTop="@drawable/tab_weixin"
- 之前我们也讲过,在rec下的含有 drawable 的都是图片资源,用于像素不同引用。
-->单张图片引用,如背景图片。直接把图片复制进去。
-->多张图片引用,如图片切换。声明一个selector.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" --------选中时,图片
android:drawable="@drawable/tabbar_contacts_hl"></item> <item android:state_checked="false" --------离开时,图片
android:drawable="@drawable/tabbar_contacts"></item> </selector>
selector 主要是用来改变ListView和Button控件的默认背景。
- android:state_selected --------> 选中
- android:state_focused --------> 获得焦点
- android:state_pressed --------> 点击
- android:state_enabled --------> 设置是否响应事件,指所有事件
- android:state_selected --------> 选中
3.android:text="@string/weixin"
- 在res下有一个values文件夹,里面有一个strings.xml
-->字符串的资源文件,供layout 布局调用。很多人会直接:text="1" ,但会有警告,就是没引用资源。
4.style="@style/radioStyle"
- 样式资源定义了用户界面(UI)的格式和外观。样式能被应用到单独的View,也能应用带整个App
-->以 name(名称) --- 属性 键值对的方式存放在 res -->values -->styles.xml下
-->当然,style一般作为一种简化代码、重复利用、作为引用 ,相当与 打包
如上面的 在每个Button下都要设置 去掉按钮、比重为1、居中显示、文本选中变颜色,那么我们可以在values.xml中声明一个style
<style name="radioStyle">
<item name="android:button">@null</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item ame="android:textColor">
@drawable/text_color</item>
</style>
在layout中就只要引用就可以了:style="@style/radioStyle" (注意对应name)
想必,知道这些,不用复制也可以自己写出来了。
下一篇:Android 微信UI、点击 http://www.cnblogs.com/hxb2016/p/6097870.html
谢谢大家的关注。万里不惜死,一朝得成功
Android RadioGroup 及资源文件 & selector的更多相关文章
- Android 开发学习进程0.17 Android资源文件selector textview显示两种不同字体
selector 是安卓资源文件的一种,它可以使按钮等实现不同状态下的不同UI,不用在代码中实现,而使用方式有两种,一种在color文件下 创建.xml可以使按钮等字体在不同状态下的变化,其二是在dr ...
- android 巧用资源文件(不断积累)
1.shape的使用 <shape xmlns:android="http://schemas.android.com/apk/res/android" > <s ...
- Android中的资源文件
最近复习Android资源文件的内容,留下点记录以备后用. Android中的资源主要是指存放在应用程序或者Framework相应包下/res中的内容.它们可以被本地化,如果必要的话会被编译成二进制文 ...
- Android学习--Assets资源文件读取及AssetManager介绍
APK安装过程 复制APK安装包到data/app目录下,解压并扫描安装包,把dex文件(Dalvik字节码)保存到dalvik-cache目录,并data/data目录下创建对应的应用 ...
- Android笔记布局资源文件
在项目的res--layout目录下的文件叫布局资源文件,用于控制页面的布局显示 在Java代码中引用布局资源我们已经很熟悉了. setContentView(R.layout.activity_ma ...
- android删除无用资源文件的python脚本
随着android项目的进行,如果没有及时删除无用的资源时安装包会越来越大,是时候整理一下废弃资源缩小压缩包了,少年! 其实判断一个资源(drawable,layout)是否没有被使用很简单,文件名( ...
- Android:Resources资源文件
Android Resoureces是res目录下的那些目录和文件,常用的有: res/drawable/ 存放图片资源,类型有: 相关使用: Android:res之shape制作圆角 Androi ...
- android如何播放资源文件夹raw中的视频
转自这里 videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/&qu ...
- Android资源文件命名规范
在复杂Android应用的开发中,资源文件的规范命名非常重要,能帮助设计人员和开发人员减小沟通成本.资源的名字尽量力求准确,可以适当长一些,但换回的价值是值得的. 关于WCC的Android开发,资源 ...
随机推荐
- ios项目总结一:开发中常用的设计模式
一.单例设计模式 1.应用场景: 程序运行期间,在内存中只有一个实例存在,主要用于资源共享,对硬件的访问等等 2.优点: 跨模块,解耦合,使用简单 3.敏捷原则: 单一职责原则 4.SDK实例: UI ...
- python: HTML中的选择器
id选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- VS2010/2012配置优化记录笔记
VS2010/2012配置优化记录笔记 在某些情况下VS2010/2012运行真的实在是太卡了,有什么办法可以提高速度吗?下面介绍几个优化策略,感兴趣的朋友可以参考下,希望可以帮助到你 有的时候V ...
- chrome的timeline中stalled问题解析
原文地址 :http://foio.github.io/chrome-stalled/ 在公司国做一个运营活动,上线后PM总是抱怨访问速度过慢,影响运营效果.然而从前端的角度来说我已经做了如下优化: ...
- 配置rt-thread开发环境(配置系统,生成系统镜像)
配置rt-thread开发环境 ===========Python============= 1.Python的下载地址:http://www.python.org/ftp/python/ 链接中有各 ...
- qq2440启动linux后插入u盘出现usb 1-1: device descriptor read/64, error -110,usb 1-1: device not accepting address 8, error -110
上位机:ubuntu14.04 64bit 下位机:qq2440 交叉编译器:arm-linux-gcc 3.4.1 下位机使用的linux内核版本:kernel2.6.13 1.插入u盘时错误信息如 ...
- 使用nodewebx进行前后端开发环境分离
下载nodewebx(windows环境) npm install nodewebx npm install inherits 为什么要下载inherits,因为nodewebx依赖它... 构建目录 ...
- JavaScript中的Math对象
Math对象中提供的计算功能执行起来比你在代码中写的js要快得多,这是它的一个很好的优点. 属性: 属性 说明 Math.E 即常量e的值 Math.LN10 ln10 Math.LN2 ln2 ...
- angular.js学习笔记
1.带ng-repeat的标签 会重复这个标签及其内部的内容,直至x循环完 比如 <tr ng-repeat="x in names | orderBy : 'Name'" ...
- AWS EC2的VPN-PPTP搭建教程(on aws redhat6.5 X64 centOS 6.5)
前些日子收到amazon的邮件通知,一年前申请的ec2到期了,一年免费的free tier没有了,放在上面的2个站已经欠费了十几美元了,不过我也不打算用了,准备重新注册账号(请不要鄙视我..) 1.注 ...