1.TextView
显示文字,相当于Panel。一般用来文本展示,继承自android.view.View,在android.widget包中。
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:Layout_height="wrap_content"
android:text="This is TextView"/>
常用属性 | 说明 | 常用值 |
---|---|---|
android:id | 给当前控件指定唯一的标识符 | 自定义 |
android:layout_width | 指定控件的宽度 | match_parent、fill_parent、wrap_content |
android:Layout_heigh | 指定控件的高度 | match_parent、fill_parent、wrap_content |
android:tex | 指定TextView中的文本显示内容 | 自定义 |
android:gravity | 指定文字的对齐方式 | top、bottom、left、right、center |
android:textSize | 指定文字大小 | 自定义 |
android:textColor | 指定文字颜色 | 自定义 |
android:layout_width和android:Layout_heigh属性中match_parent 和fill_parent的意义相同,推荐使用match_parent 表示让当前的控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小,wrap_content表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小,也可以对控件的宽高指定一个固定的大小,但是这样做在不同屏幕大小的手机上出现适配问题 。
android:gravity属性中可用 | 来指定多个值,center效果等同于 center_vertical | center_horizontal,表示文字在垂直和水平方向都居中对齐
2.Button
Button是最常用的按钮,继承自android.widget.TextView,在android.widget包中。他的常用子类CheckBox,RadioButton, ToggleButton。
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:Layout_height="wrap_content"
android:text="Button"
android:textAllCaps="false"/>
常用属性 | 说明 | 常用值 |
---|---|---|
android:id | 给当前控件指定唯一的标识符 | 自定义 |
android:layout_width | 指定控件的宽度 | match_parent、fill_parent、wrap_content |
android:Layout_heigh | 指定控件的高度 | match_parent、fill_parent、wrap_content |
android:text | 指定TextView中的文本显示内容 | 自定义 |
android:textAllCaps | 是否将英文字母自动转换成大写 | true–自动转换、false–不转换 |
3.EditText
输入框,可编辑,可设置软键盘方式。继承自android.widget.TextView,在android.widget包中。
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:Layout_height="wrap_content"
android:hint="Type something here"
android:maxLines="2"/>
常用属性 | 说明 | 常用值 |
---|---|---|
android:id | 给当前控件指定唯一的标识符 | 自定义 |
android:layout_width | 指定控件的宽度 | match_parent、fill_parent、wrap_content |
android:Layout_heigh | 指定控件的高度 | match_parent、fill_parent、wrap_content |
android:hint | 指定了一段提示文本,当我们输入任何内容,这段文本就会自动消失 | 自定义 |
android:maxLine | 指定了EditText最大行数为2行,这样当输入的内容超过两行时,文本就会向上滚动,而EditText不会再继续拉伸 | 自定义 |
4.ImageView
ImageView控件负责显示图片,其图片的来源可以是在资源文件中的id,也可以是Drawable对象或者位图对象。还可以是Content Provider的URI。
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:Layout_height="wrap_content"
android:src="@drawable/img"/>
常用属性 | 说明 | 常用值 |
---|---|---|
android:id | 给当前控件指定唯一的标识符 | 自定义 |
android:layout_width | 指定控件的宽度 | match_parent、fill_parent、wrap_content |
android:Layout_heigh | 指定控件的高度 | match_parent、fill_parent、wrap_content |
android:src | 给ImageView指定一张图片 | 自定义 |
5.ProgressBar
ProgressBar 用于在界面上显示一个进度条,表示我们的程序正在加载一些数据,当程序运行后,会看到屏幕中有一个圆形进度条正在旋转。
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:Layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"/>
常用属性 | 说明 | 常用值 |
---|---|---|
android:id | 给当前控件指定唯一的标识符 | 自定义 |
android:layout_width | 指定控件的宽度 | match_parent、fill_parent、wrap_content |
android:Layout_heigh | 指定控件的高度 | match_parent、fill_parent、wrap_content |
style | 设置进度条样式 | 默认为圆形、progressBarStyleHorizontal(水平进度条) |
android:max | 设置进度条最大值 | 自定义 |
6.AlertDialog
在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽其他控件的交互能力,因此一般AlertDialog都是用于提示一些非常重要的内容或者警告信息,比如为了防止用户误删内容,在删除前弹出一个确认对话框
//绑定监听事件(点击已经设置好的按钮后弹出对话框)
case R.id.btnAlert:
AlertDialog.Builder dia=new AlertDialog.Builder(SecondActivity.this);
dia.setTitle("This is Dialog");//设置标题
dia.setMessage("Something important.");//设置内容
//点击确定按钮
dia.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(SecondActivity.this,"OK" , Toast.LENGTH_SHORT).show();
}
});
//点击否定按钮
dia.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(SecondActivity.this, "Cancel", Toast.LENGTH_SHORT).show();
}
});
//显示对话框
dia.show();
break;
7.ProgressDialog
ProgressDialog和AlertDialog有点类似,都可以在界面上弹出一个对话框,都能够屏蔽掉其他控件的交互能力,不同的是,ProgressDialog会在对话框中显示一个进度条,一般是用于表示当前操作比较耗时,让用户耐心地等待
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("This is ProgressDialog"); //设置标题
progressDialog.setMessage("Loading..."); //设置内容
progressDialog.setCancelable(true);
//显示对话框
progressDialog.show();
注意:如果在setCancelable()中传入了false,表示ProgressDialog是不能通过Back键取消掉的,这时一定要在代码中做好控制,
当数据加载完成后必须要调用ProgressDialog的dismiss()方法来关闭对话框,否则ProgressDialog将会一直存在