您的位置:首页 > 其它

ImageView、RadioButton、CheckBox等控件学习

2015-09-22 21:40 501 查看
1、ImageView用于编辑显示图片控件,在调整一张片的情况下,属性layout_width和layout_height应设置为match_parent,不然可能会使图片无法满屏。

同时设置图片是用属性src,虽然用background同样能让一张图片设置在屏幕中,但是,当调整图片时可能会使图片变形。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:src="@drawable/animal"

此段代码则可表示放上一张图片,若放多张图片,则需要使用gridview调用适配器完成,使图片达到缩放不变形的效果,这个下次再另外叙述

2、RadioButton单选控件,但在使用时应该配合RadioGroup一起使用,不然会出现同时选中的情况,系统默认的格式,例如

<RadioGroup

android:id="@+id/sex_radio"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

<!-- android:checked="true" 设置默认选中 -->

<RadioButton

android:id="@+id/man_radio"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/man_radio"

android:textSize="20sp"/>

<RadioButton

android:id="@+id/woman_radio"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/woman_radio"

android:textSize="20sp"/>

</RadioGroup>

RadioButton自定义格式可通过自定义背景图形格式来形成

<RadioButton

android:id="@+id/man_2_radio"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/man_radio"

android:background="@drawable/sex_button_selector" <!-- 自定义资源selector 显示选中与没有选中的区别,定义自己的格式 -->

android:gravity="center"

android:button="@null" <!-- 消除本来系统定义的格式 -->

android:layout_marginTop="10dp"

android:textSize="20sp"

/>

3、CheckBox多选控件
实现代码如下

<CheckBox

android:id="@+id/one"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="onChickClick" <!-- 定义点击方法名,在Java代码中定义此方法,当选中时获取信息 -->

android:text="@string/first" />

自定义格式同RadioButton

4、ToggleButton和Switch,开关控件,前者为点击时改变状态,后者为滑动改变状态,

android:textOff="关"

android:textOn="开"

需要说的是当要获取switch的值时,尽量不要用onclick属性来定义方法名来获取,因为此时为点击才回获取值,

滑动时则不能获取。因此需要在Java代码中以实现接口的形式调用OnCheckedChangeListener()方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: