本章主要介绍常用的控件和基本的布局方法
- 常用控件
1. TextView
在界面上显示一段文本信息
< TextView
android: id= "@+id/textView_Test"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: gravity= "center"
android: text= "Hello World!"
android: textSize= "24sp"
android: textColor= "#00ff00"
/ >
2. Button
在界面上显示一个按钮
< Button
android: id= "@+id/button_Test"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: text= "Hello"
android: textAllCaps= "false"
/ >
3.EditText
可编辑的文本,允许用户在控件里输入和编辑
< EditText
android: id= "@+id/EditText_Test"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: hint= "Type something here"
android: maxLines= "2" / >
4.ImageView
在界面上显示图片
< ImageView
android: id= "@+id/imageView_Test"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: src= "@drawable/ccc" / >
5.ProgressBar
在界面上显示一个进度条
< ProgressBar
android: id= "@+id/progressBar_Test"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
style= "?android:attr/progressBarStyleHorizontal"
android: max= "100"
/ >
6.AlertDialog
在当前界面弹出一个对话框,不同于上面的,这个是在程序中写的
AlertDialog. Builder dialog = new AlertDialog. Builder ( MainActivity. this ) ;
dialog. setTitle ( "This is a dialog" ) ;
dialog. setMessage ( "Message" ) ;
dialog. setCancelable ( false ) ;
dialog. setPositiveButton ( "OK" , new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialogInterface, int i) {
}
} ) ;
dialog. setNegativeButton ( "Cancel" , new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialogInterface, int i) {
}
} ) ;
dialog. shou ( ) ;
7.ProgressDialog
在当前界面弹出一个有进度条的对话框,也是在程序中完成
ProgressDialog progressDialog = new ProgressDialog ( MainActivity. this ) ;
progressDialog. setTitle ( "This is a ProgressDialog" ) ;
progressDialog. setMessage ( "Loading..." ) ;
progressDialog. setCancelable ( false ) ;
progressDialog. show ( ) ;
- 常用布局
1.LinearLayout
线性布局,将他所包含的控件在线性方向上依次排列
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout
xmlns: android= "http://schemas.android.com/apk/res/android"
android: orientation= "horizontal"
android: layout_width= "match_parent"
android: layout_height= "match_parent" >
< Button
android: id= "@+id/button_Test"
android: layout_width= "0dp"
android: layout_weight= "1"
android: layout_gravity= "center"
android: layout_height= "wrap_content"
android: text= "Hello"
android: textAllCaps= "false"
/ >
< EditText
android: id= "@+id/EditText_Test"
android: layout_width= "0dp"
android: layout_weight= "1"
android: layout_height= "wrap_content"
android: hint= "Type something here"
android: maxLines= "2" / >
< / LinearLayout>
2.RelativeLayout
相对布局,可通过相对于父布局或相对于其他控件来约束控件位置
< ? xml version= "1.0" encoding= "utf-8" ? >
< RelativeLayout
xmlns: android= "http://schemas.android.com/apk/res/android"
android: layout_width= "match_parent"
android: layout_height= "match_parent" >
< Button
android: id= "@+id/button1"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: layout_alignParentLeft= "true"
android: layout_alignParentTop= "true"
android: text= "Hello"
android: textAllCaps= "false"
/ >
< Button
android: id= "@+id/button2"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: layout_alignParentRight= "true"
android: layout_alignParentBottom= "true"
android: text= "Hello"
android: textAllCaps= "false"
/ >
< Button
android: id= "@+id/button3"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: layout_centerInParent= "true"
android: text= "Hello"
android: textAllCaps= "false"
/ >
< Button
android: id= "@+id/button4"
android: