zoukankan      html  css  js  c++  java
  • Android:TextView控件

    3.2.1    TextView

    TextView 可以说是 Android 中最简单的一个控件了,你在前面其实也已经和它打过了一 些打交道。它主要用于在界面上显示一段文本信息,比如你在第一章看到的 Hello world!下 面我们就来看一看关于 TextView 的更多用法。

    将 activity_main.xml 中的代码改成如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

    <TextView android:id="@+id/text_view" 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 中所有的控件都具有这 两个属性,可选值有三种 match_parent 、fill_parent 和 wrap_content,其中 match_parent 和 fill_parent 的意义相同,现在官方更加推荐使用 match_parent。match_parent 表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小。wrap_content 表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小。所以 上面的代码就表示让 TextView 的宽度和父布局一样宽,也就是手机屏幕的宽度,让 TextView 的高度足够包含住里面的内容就行。当然除了使用上述值,你也可以对控件的宽和高指定一 个固定的大小,但是这样做有时会在不同手机屏幕的适配方面出现问题。接下来我们通过 android:text 指定了 TextView 中显示的文本内容,现在运行程序,效果如图 3.1 所示。

     

    图   3.1

    虽然指定的文本内容是正常显示了,不过我们好像没看出来 TextView 的宽度是和屏幕 一样宽的。其实这是由于 TextView 中的文字默认是居左上角对齐的,虽然 TextView 的宽度 充满了整个屏幕,可是从效果上完全看不出来。现在我们修改 TextView 的文字对齐方式, 如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"

    android:orientation="vertical" >

    <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="This is TextView" />

    </LinearLayout>

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

     

    图   3.2

    这也说明了,TextView 的宽度确实是和屏幕宽度一样的。 另外我们还可以对 TextView 中文字的大小和颜色进行修改,如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

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

    </LinearLayout>

    通过 android:textSize 属性可以指定文字的大小,通过 android:textColor 属性可以指定文 字的颜色。重新运行程序,效果如图 3.3 所示。

     

    图   3.3

    当然 TextView 中还有很多其他的属性,这里我就不再一一介绍了,需要用到的时候去 查阅文档就可以了。

  • 相关阅读:
    SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)
    麦咖啡导致电脑不能上网
    SharePoint 2013 Central Admin 不能打开
    SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)
    SharePoint 2013 APP 开发示例 系列
    synthesize(合成) keyword in IOS
    Git Cmd
    简单的正则匹配
    Dropbox
    SQL Server Replication
  • 原文地址:https://www.cnblogs.com/zgqys1980/p/5114230.html
Copyright © 2011-2022 走看看