【Android】27、常见控件的使用方法——TextView
来源:互联网 发布:长江师范学院网络教学 编辑:程序博客网 时间:2024/05/22 13:30
本篇博文最后修改时间:2016年5月30日,10:31。
一、简介
本篇介绍常见控件——TextView的使用方法。
二、实验平台
系统版本:Windows7 家庭普通版 32位操作系统。
三、版权声明
博主:思跡
声明:喝水不忘挖井人,转载请注明出处。
原文地址:http://blog.csdn.net/omoiato
联系方式:315878825@qq.com
Java零基础入门交流群:541462902
四、常见控件的使用方法——TextView
Android 给我们提供了大量的UI 控件,合理地使用这些控件,
就可以非常轻松地编写出相当不错的界面。
下面我们就挑选几种常用的控件,详细介绍一下它们的使用方法。
首先新建一个UIWidgetTest 项目,简单起见,
我们还是允许ADT 自动创建活动,活动名和布局名都使用默认值。
别忘了将其他不相关的项目都关闭掉,始终养成这样一个良好的习惯。
TextView
1、简介:
TextView 可以说是Android 中最简单的一个控件了,
我们在前面其实也已经和它打过了一些打交道。它主要用于在界面上显示一段文本信息,
比如我们在第一章看到的Hello world!
2、用法:
下面我们就来看一看关于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 中显示的文本内容
②修改TextView 的文字对齐方式
虽然指定的文本内容是正常显示了,不过我们好像没看出来TextView 的宽度是和屏幕一样宽的。
其实这是由于TextView 中的文字默认是居左上角对齐的,
虽然TextView 的宽度充满了整个屏幕,可是从效果上完全看不出来。
修改TextView 的文字对齐方式如下所示:
<pre class="html" name="code"><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",
表示文字在垂直和水平方向都居中对齐。
③修改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 属性可以指定文字的颜色。
- 【Android】27、常见控件的使用方法——TextView
- 文章标题:Android常见控件TextView的使用方法
- 【Android】28、常见控件的使用方法——Button
- 【Android】29、常见控件的使用方法——EditText
- 【Android】30、常见控件的使用方法——ImageView
- 【Android】31、常见控件的使用方法——ProgressBar
- 【Android】32、常见控件的使用方法——AlertDialog
- 【Android】33、常见控件的使用方法——ProgressDialog
- android-常见控件的使用方法
- Android TextView的使用方法
- 常见控件的使用方法
- Android常见的几个控件(TextView,EditText,Button)
- Android的常见控件(TextView、EditText、Button、Menu)使用
- Android学习-常见的UI控件 TextView、EditText和ImageView
- Android学习笔记·常见控件TextView
- Android 控件之TextView常见使用问题
- 1.Android基础:常见控件----->TextView
- 文章标题:Android常见控件ImageView的使用方法
- "巴卡斯杯" 中国大学生程序设计竞赛 - 女生专场(重现)解题思路
- Android 6.0+ 运行时权限探索
- 使用Myeclipse 10开发基于JAX-WS的Web service实例
- 微机原理中$求内存数据个数
- ajax库类以及使用方法
- 【Android】27、常见控件的使用方法——TextView
- 网站常用效果
- 常见的八种导致 APP 内存泄漏的问题
- typedef的用法
- 4. Median of Two Sorted Arrays
- 数组部分元素反转--三次翻转法
- js上传文件(图片)限制格式及大小为3M · 观止
- org.apache.jasper.JasperException: java.lang.IllegalStateException: No output folder
- windows和linux下的文件路径表示小结