Android常用控件
Androd常用的几种控件:
文本类控件
TextView 负责展示文本,非编辑
EditText 可编辑文本控件
按钮类控件
Button 按钮
ImageButton 图片按钮
RadioButton与RadioGroup 单选按钮
CheckBox 复选按钮
图片控件
ImageView 负责显示图片
进度条控件
ProgressBar 进度条
主要的一些属性:
//控件idandroid:id = "@+id/xxx" @+id/xxx表示新增控件命名为xxx
//宽度与高度android:layout_width="wrap_content" //wrap_content或者match_parentandroid:layout_height="wrap_content" //wrap_content或者match_parent
//文本文字 android:text="@string/hello_world" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素
//字体大小android:textSize="24sp" //以sp为单位
//字体颜色android:textColor="#0000FF" //RGB颜色
//字体格式android:textStyle="normal" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal
//文本显示位置android:gravity="center" //来指定文字的对齐方式,可选值有 top、bottom、left、right、center 等
//是否只在一行内显示全部内容android:singleLine="true" //true或者false,默认为false
更多属性可参考TextView
2:EditText
//控件idandroid:id = "@+id/xxx" @+id/xxx表示新增控件命名为xxx
//宽度与高度android:layout_width="wrap_content" //wrap_content或者match_parentandroid:layout_height="wrap_content" //wrap_content或者match_parent
//文本文字 android:text="@string/hello_world" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素
//文本提示内容android:hint="hello_world" //android:text和android:hint区别是后者只是提示作用,真正需要输入的时候提示的内容会消失
//字体大小android:textSize="24sp" //以sp为单位
//字体颜色android:textColor="#0000FF" //RGB颜色
//字体格式android:textStyle="normal" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal
//文本显示位置android:gravity="center" //来指定文字的对齐方式,可选值有 top、bottom、left、right、center 等
//是否只在一行内显示全部内容android:singleLine="true" //true或者false,默认为false
//输入内容设置为password类型android:password="true" //输入的内容会变成······
//输入内容设置为phoneNumber类型android:phoneNumber="true" //只能输入数字
//设定光标为显示/隐藏android:cursorVisible = "false" //true或者false,默认为true显示
更多属性可参考EditText
通常用法:
? super.findViewById(id)得到在layout中声明的Button的引用,setOnClickListener(View.OnClickListener)添加监听。然后再View.OnClickListener监听器中使用v.equals(View)方法判断哪个按钮被按下,进行分别处理。
ImageButton继承自ImageView类,与Button之间的最大区别在于ImageButton中没有text属性。ImageButton控件中设置按钮中显示的图片可以通过android:src属性来设置。也可以通过setImageResource(int)来设置。
ImageView常用属性(也是ImageButton继承具有的属性):
4:ImageView图片控件
ImageView控件负责显示图片,其图片的来源可以是在资源文件中的id,也可以是Drawable对象或者位图对象。还可以是Content Provider的URI。
常用属性:
Android:adjustViewBounds |
设置是否需要ImageView调整自己的边界,保证图片的显示比例 |
Android:maxHeight |
最大高度 |
Android:maxWidth |
最大宽度 |
Android:src |
图片路径 |
Android:scaleType |
调整或移动图片 |
常用方法:
setAlpha(int) |
设置ImageView透明度 |
setImageBitmap(Bitmap) |
设置ImageView显示的内容为Bitmap对象 |
setImageDrawable(Drawable) |
设置ImageView所显示内容为Drawable |
setImageURI(Uri) |
设置ImageView所显示内容为Uri |
setSelected(boolean) |
设置ImageView的选择状态 |
常用方法:
isChecked()检查是否被选中。
监听按钮状态更改,需要添加setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener);
android.widget. RadioButton单选按钮,继承自android.widget.CompoundButton,在android.widget包中。
通常用法:
单选按钮要声明在RadioGroup,RadioGroup是流式布局android.widget.LinearLayout的子类。
单选按钮状态更改的监听,是要给他的RadioGroup添加setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener)监听器。注意监听器类型和CheckBox是不一样的。
ProgressBar 用于在界面上显示一个进度条,表示我们的程序正在加载一些数据,运行程序,会看到屏幕中有一个圆形进度条正在旋转。
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="wrap_content" //默认是圆形进度条,可以知道样式设置为水平进度条
style="?android:attr/progressBarStyleHorizontal"/> //指定成水平进度条后,我们还可以通过 android:max属性给进度条设置一个最大值,然后在代码中动态地更改进度条的进度
android:max="100"