Android常用控件

来源:互联网 发布:淘宝哪家情侣装好看 编辑:程序博客网 时间:2024/06/14 19:03

Android基本布局和常用控件的日常使用

1.ScrollView

这个控件的使用一般是为了是界面可以有滚动条,可以为垂直方向的滚动,需要注意的是这个控件只能放在一个界面的最外层,并且这个控件内只能放一个布局文件。

2.LinearLayout

这个控件使用时需要注意它的方向是需要水平的还是需要垂直的。

3.ImageView

 <ImageView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            //android:src = "@drawable/图片名称"            android:src="@drawable/selector_position"            android:visibility="visible" />

这个控件使用时需要注意的是图片的切换:

可以设置为drawable目录下自己定义的xml文件,在文件中生命按下和未按下的图片显示

<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/map_position1" android:state_pressed="true"></item><item android:drawable="@drawable/map_position0"></item></selector> 

也可以设置为drawable文件夹下的图片名称

//android:src = "@drawable/图片名称"

4.TableLayout

<TableLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:stretchColumns="0,2"     android:shrinkColumns="1">

需要注意定义中的第三行和第四行

第三行:stretchColumns属性,将这个tablelayout中的相应行进行拉伸,使其将剩余的宽度控件占满—有空就填满

第四行:shrinkColumns属性,如果有一行的内容过多需要换行时使用,此属性可以在保证其余行最佳尺寸的情况下,对当前行进行换行—放不下要换行

 <TableRow        android:layout_width="match_parent"        android:layout_height="wrap_content" >

width属性一般设置为match_parent

height属性一般设置为wrap_content

5.DatePicker

<DatePicker        android:layout_width="wrap_content"        android:layout_height="wrap_content" />

这里写图片描述

6.TimePicker

<TimePicker         android:layout_width="wrap_content"

这里写图片描述

上述两个控件在使用时需要什么样的样式自行调用,需要注意的是他们在不同的Android版本上有很不一样的界面

7.SeekBar

<SeekBar    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:max="100"    android:progress="30"/>

这里写图片描述
max属性设置总长,progress属性设置已经拉取的长度的比例

8.ProgressBar

<ProgressBar    style="@android:styleWidget.ProgressBar.Horizontal"    android:layout_width="match_parent"    android:layout_height="wrap_content"     android:max="100"    android:progress="30"

这里写图片描述
max,progress属性和上述控件相同,secondaryProgress表示将要progress的进度,表示为灰度显示,style属性表示这个控件为横向的进度条形状

9.HorizontalScrollView

<HorizontalScrollView        android:layout_width="match_parent"        android:layout_height="wrap_content" >

这个控件表示水平方向上的有滚动条,其他属性和ScrollView保持一致

10.ToggleButton

<ToggleButton    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:checked="true"    android:textOn="@string/basketball"    android:textOff="@string/football"/>

这个控件可以在选中和未被选中时显示不同的文字内容

11.CheckBox

<CheckBox    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="@drawable/selector_checkbox2"    android:button="@null"    android:checked="true"    android:gravity="center"    android:text="是否开启网络" />    

复选框

background属性可以为图片资源或者自己定义的xml文件

将button属性设置为null时不显示最前边的小框,为了修改这个小框的图片,做法和设置background的方法一致

gravity属性文字放在background的位置

12.ImageView,TextView,EditText

<ImageView    android:id="@+id/map_position"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/selector_position"    android:visibility="visible" /><TextView    android:id="@+id/map_title"    android:layout_width="100dp"    android:layout_height="wrap_content"    android:background="@color/yellow"    android:gravity="center"    android:padding="5dp"    android:text="@string/hello_world"    android:textColor="@color/red"    android:textSize="@dimen/font_middle" /><EditText    android:layout_width="match_parent"    android:layout_height="match_parent"    android:hint="请输入密码"    android:inputType="textPassword"    android:maxLength="8"    android:padding="5dp"    android:singleLine="true"    android:textColor="@color/red"    android:textColorHint="@color/yellow"    android:textSize="@dimen/font_middle" />

13.RadioGroup

<RadioGroup    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:button="@drawable/selector_checkbox"        android:text="水果" />    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:button="@drawable/selector_checkbox"        android:checked="true"        android:text="咖啡" />    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/selector_checkbox2"        android:button="@null"        android:gravity="center"        android:text="猴子" /></RadioGroup>

需要设置方向为水平,在RadioGroup中放置RadioButton就可以实现单选的功能

0 0