Android学习(6)

Android UI开发

常用控件

TextView
1
2
3
4
5
6
7
8
9
10
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView" />
</LinearLayout>

让文字居中

1
2
3
4
5
6
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView"
android:gravity="center"/>

我们使用android:gravity 来指定文字的对齐方式,可选值有top、bottom、left、right、center等,可以用“|”来同时指定多个值,这里我们指定的center,效果等同于center_vertical|center_horizontal,表示文字在垂直和水平方向都居中对齐。

Button

1
2
3
4
5
6
7
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button_1"
android:text="Button 1"
android:textSize="24sp"
android:textAllCaps="false"/>

EdiText

1
2
3
4
5
6
7
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="2"
android:hint="请输入"/>

ImageView

1
2
3
4
5
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/im_0"/>

ProgressBar

1
2
3
4
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progress_bar"/>

AlertDialog

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

首先应该通过AlertDialog.Builder创建一个AlertDialog的实例,然后为这个对话框设置标题、内容等关键属性。

ProgressDialog

PregressDialog和AlertDIalog很相似,但是ProgressDialog会在对话框显示进度,一般用于表示当前操作需要耗费一定时间,请用户耐心等待。

1
2
3
4
5
6
7
8
9
switch(v.getId()) {
case R.id.button_1:
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("wait");
progressDialog.setMessage("Loading……");
progressDialog.setCancelable(true);
progressDialog.show();
break;
default: