目录
4.2 常用控件的使用方法
Android 给我们提供了大量的 UI 控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,下面我们就挑选几种常用的控件,详细介绍一下它们的使用方法。
首先新建一个 UIWidgetTest 项目。简单起见,我们还是允许 Android Studio 自动创建 Activity ,Activity 名和布局名都使用默认值。
4.2.1 TextView
TextView 可以说是 Android 中最简单的一个控件了,你在前面其实已经和它打过一些交道了。 它主要用于在界面上显示一段文本信息,比如你在第 1 章看到的 Hello world !
下面我们就来看一看 TextView 的更多用法,将 activity_main.xml 中的代码改成如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#DDDDDD"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is TextView"
/>
</LinearLayout>
外面的 LinearLayout 先忽略不看,在 TextView 中我们使用 android:id 给当前控件定义了一个唯一标识符,这个属性在上一章中已经讲解过了。然后使用 android:layout_width 和 android:layout_height 指定了控件的宽度和高度。Android 中所有的控件都具有这两个属性,可选值有3种:match_parent、wrap_content 和固定值。match_parent 表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。wrap_content 表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。固定值表示表示给控件指定一个固定的尺寸,单位一般用 dp ,这是一种屏幕密度无关的尺寸单位,可以保证在不同分辨率的手机上显示效果尽可能地一致,如 50 dp 就是一个有效的固定值。
所以上面的代码就表示让 TextView 的宽度和父布局一样宽,也就是手机屏幕的宽度,让 TextView 的高度足够包含住里面的内容就行。现在运行程序,效果如 图4.1 所示。

虽然指定的文本内容正常显示了,不过我们好像没看出来 TextView 的宽度是和屏幕一样宽的。 其实这是由于 TextView 中的文字默认是居左上角对齐的,虽然 TextView 的宽度充满了整个屏幕,可是由于文字内容不够长,所以从效果上完全看不出来。现在我们修改 TextView 的文字对 齐方式,如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#DDDDDD"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is TextView"
/>
</LinearLayout>
我们使用 android:gravity 来指定文字的对齐方式,可选值有 top、bottom、start、 end、center等,可以用 “|” 来同时指定多个值,这里我们指定的是 "center",效果等同于 "center_vertical | center_horizontal",表示文字在垂直和水平方向都居中对齐。 现在重新运行程序,效果如 图4.2 所示。

这也说明了 TextView 的宽度确实是和屏幕宽度一样的。
另外,我们还可以对 TextView 中文字的颜色和大小进行修改,如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#DDDDDD"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#00ff00"
android:textSize="24sp"
android:text="This is TextView"
/>
</LinearLayout>
通过 android:textColor 属性可以指定文字的颜色,通过 android:textSize 属性可以指定文字的大小。文字大小要使用 sp 作为单位,这样当用户在系统中修改了文字显示尺寸时,应用程序中的文字大小也会跟着变化。重新运行程序,效果如 图4.3 所示。

当然 TextView 中还有很多其他的属性,这里我就不再一一介绍了,你需要用到的时候去查阅文档就可以了。
小贴士:
android:layout_width 指定了控件的宽度
android:layout_height 指定了控件的高度
match_parent 表示让当前控件的大小和父布局的大小一样
wrap_content 表示让当前控件的大小能够刚好包含住里面的内容
4.2.2 Button
Button 是程序用于和用户进行交互的一个重要控件,相信你对这个控件已经非常熟悉了,因为我们在上一章用了很多次。它可配置的属性和 TextView 是差不多的,我们可以在 activity_main.xml 中这样加入Button :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:background="#DDDDDD"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content