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 中还有很多其他的属性,这里我就不再一一介绍了,需要用到的时候去 查阅文档就可以了。

Android:TextView控件的更多相关文章

  1. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式

    注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...

  2. android中的TextView控件

    我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导.  一.TextView的基本使用 Te ...

  3. android课程第一节(TextView控件使用)

    TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...

  4. android软件开发之TextView控件常用属性

    TextView控件 text属性,设置显示的文本 textColor:设置文本颜色 textSize:设置文本字体大小 autoLink:设置文本为电话,URL连接等的时候是否显示为可点击的链接 c ...

  5. 如何设置TextView控件的背景透明度和字体透明度

    如何设置TextView控件的背景透明度和字体透明度 设计师给的标注都是类似这样的: 字号:26 颜色:#000000 透明度:80% 其实,程序上只要需要一个色值就OK了,那么这个色值我如何计算呢? ...

  6. 两个TextView控件居中显示

    通过一个线性布局将两个TextView控件包装在一起,设置LinearLayout的layout_centerInParent属性为true即可.代码如下 <LinearLayout andro ...

  7. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  8. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  9. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

随机推荐

  1. Unicode范围预览

    链接: https://www.zhangxinxu.com/study/201611/show-character-by-charcode.php?range=4E00-9FBB 备注: Unico ...

  2. 测试开发之Django——No8.Django中的视图与URL配置

    在Django中,页面内容是靠views function(视图函数)来产生,URL定义在URLconf中. 这是一个纯净项目的url配置文件,他存在的目录,是与setting文件在同一个目录中. u ...

  3. TensorFlow 框架

    TensorFlow TensorFlow核心程序由2个独立部分组成:   a:Building the computational graph构建计算图   b:Running the comput ...

  4. linux 下 eclipse 安装

    下载: 官网选择相应安装包下载,我这里下了tar.gz包 安装: tar xzvf eclipse-inst-linux64.tar.gz 设置环境变量 export JAVA_HOME=/usr/l ...

  5. java 判断字符串什么编码类型

    public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equ ...

  6. Ext.js项目(一)

    这个项目整体采用代码生成器生成,具体看下图:

  7. 【C++ Primer | 19】运行类型识别

    运行类型识别 一.使用RTTI dynamic_cast运算符的调用形式如下所示: dynamic_cast<type*>(e) //e是指针 dynamic_cast<type&a ...

  8. 【【C++ Primer 第15章】 虚析构函数

    学习资料 • C++中基类的析构函数为什么要用virtual虚析构函数 虚析构函数 1. 正文 直接的讲,C++中基类采用virtual虚析构函数是为了防止内存泄漏.具体地说,如果派生类中申请了内存空 ...

  9. 【C++】拷贝构造函数(深拷贝,浅拷贝)详解

    一.什么是拷贝构造函数  首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: ; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量. 下面看一个类对 ...

  10. 依赖倒置原则(Dependence Inversion Principle,DIP)

    依赖倒转原则就是 A.要依赖于抽象,不要依赖于实现.(Abstractions should not depend upon details. Details should depend upon a ...