Android之路5.常用控件的使用

来源:互联网 发布:linux结束所有进程命令 编辑:程序博客网 时间:2024/06/07 07:40

1.TextView

 <TextView        android:id="@+id/text_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textSize="24sp"        android:textColor="#00ff00"        android:text="This is TextView"/>

id定义一个唯一标识符

layout_width和layout_height指定控件的宽度和高度

text指定TextView中显示的文本内容

gravity指定文字的对齐方式

textSize指定文字的大小

textColor指定文字的颜色

2.Button

<Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Button"        android:textAllCaps="false"/>

textAllCaps设置英文字母自动大写转换

3.EditText

EditText允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理。

<EditText        android:id="@+id/edit_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="Type something here"        android:maxLines="2"/>

hint属性指定一段提示性的文本,输入任何内容文本会消失

maxLines指定EditText的最大行数,当输入的内容超过最大行数,文本就会向上滚动,而不会再继续拉伸

4.ImageView

ImageView是用于在界面上展示图片的一个控件

<ImageView        android:id="@+id/image_view"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:src="@mipmap/img1"/>

src属性给ImageView指定了一张图片,由于图片的宽和高都是未知的,所以将ImageView的宽和高都设定为wrap_content,这样就保证了不管图片的尺寸是多少,图片都可以完整的展示出来。

动态改变图片 P97

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"/>

visibility可以设置控件的可选属性

getVisibility()来判断ProgressBar是否可见

style属性指定进度条的样式

6.AlertDialog

AlertDialog可以在当前的界面弹出一个对话框,这个对话框是置于所有界面元素之上的,能够屏蔽掉其他控件的交互能力,因此AlertDialog一般都是用于提示一些非常重要的内容或者警告信息。

 AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);                dialog.setTitle("This is Dialog");                dialog.setMessage("Something important");                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.show();

首先通过AlertDialog.Builder创建一个AlertDialog的实例,然后可以为这个对话框设置标题、内容、可否取消等属性,接下来调用setPositiveButton()方法为对话框设置确定按钮的点击事件,调用setNegativeButton()方法设置取消按钮的点击事件,最后调用show()方法将对话框显示出来。

7.ProgressDialog

ProgressDialog和AlertDialog有点类似,都可以在界面上弹出一个对话框,都能够屏蔽掉其他控件的交互能力。不同的是,ProgressDialog会在对话框中显示一个进度条,一般用于表示当前的操作比较耗时,让用户耐心等待。

 ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);                progressDialog.setTitle("This is ProgressDialog");                progressDialog.setMessage("Loading...");                progressDialog.setCancelable(true);                progressDialog.show();