Android笔记(十三) Android中的基本组件——文本
Android中常用的文本组件有 普通文本框(TextView)和编辑框(EditText)两种
EditText是TextView的子类,作用就是在界面上显示文本,区别是EditText允许用户编辑文本框内的内容。
简单示例:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
> <TableRow>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
android:text="用户名:"
android:textSize="10pt"
android:width="20pt"
android:background="@drawable/username"/> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写登录帐号"
android:layout_weight="9"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="10pt"
android:background="@drawable/password"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码"
android:password="true"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="10pt"
android:background="@drawable/mobilephone"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"/>
</TableLayout>
运行结果:


更改TextView的字体:
1. 使用属性Android:typeface=“sans|serif|monospace" 来更改字体,Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace"
2. 引入其他字体,首先要将字体文件保存在assets/fonts/目录下,然后修改Activity
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/youyuan.TTF");
TextView tv = (TextView)findViewById(R.id.username);
tv.setTypeface(tf);
运行结果为:

其他XML属性
|
属性名称 |
描述 |
|
android:autoLink |
设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。 可选值(none/web/email/phone/map/all) |
|
android:autoText |
如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。 |
|
android:bufferType |
指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符, 也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式 |
|
android:capitalize |
设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。 |
|
android:cursorVisible |
设定光标为显示/隐藏,默认显示。 |
|
android:digits |
设置允许输入哪些字符。如“1234567890.+-*/%\n()” |
|
android:drawableBottom |
在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色, 并且同时和background使用时覆盖后者。 |
|
android:drawableLeft |
在text的左边输出一个drawable,如图片。 |
|
android:drawablePadding |
设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、 drawableBottom一起使用,可设置为负数,单独使用没有效果。 |
|
android:drawableRight |
在text的右边输出一个drawable,如图片。 |
|
android:drawableTop |
在text的正上方输出一个drawable,如图片。 |
|
android:editable |
设置是否可编辑。这里无效果,参见EditView。 |
|
android:editorExtras |
设置文本的额外的输入数据。在EditView再讨论。 |
|
android:ellipsize |
设置当文字过长时,该控件该如何显示。有如下值设置: ”start”—–省略号显示在开头; ”end”——省略号显示在结尾; ”middle”—-省略号显示在中间; ”marquee” ——以跑马灯的方式显示(动画横向移动) |
|
android:freezesText |
设置保存文本的内容以及光标的位置。 |
|
android:gravity |
设置文本位置,如设置成“center”,文本将居中显示。 |
|
android:hint |
Text为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。 此属性在EditView中使用,但是这里也可以用。 |
|
android:imeOptions |
附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”, 而不设置默认是一个回车符号。这个在EditView中再详细说明,此处无用。 |
|
android:imeActionId |
设置IME动作ID |
|
android:imeActionLabel |
设置IME动作标签。在EditView再做说明。 |
|
android:includeFontPadding |
设置文本是否包含顶部和底部额外空白,默认为true。 |
|
android:inputMethod |
为文本指定输入法,需要完全限定名(完整的包名)。 例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。 |
|
android:inputType |
设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。 |
|
android:marqueeRepeatLimit |
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为 |
|
android:ems |
设置TextView的宽度为N个字符的宽度。 |
|
android:maxEms |
设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 |
|
android:minEms |
设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。 |
|
android:maxLength |
限制显示的文本长度,超出部分不显示。 |
|
android:lines |
设置文本的行数,设置两行就显示两行,即使第二行没有数据。 |
|
android:maxLines |
设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。 |
|
android:minLines |
设置文本的最小行数,与lines类似。 |
|
android:linksClickable |
设置链接是否点击连接,即使设置了autoLink。 |
|
android:lineSpacingExtra |
设置行间距。 |
|
android:lineSpacingMultiplier |
设置行间距的倍数。如”1.2” |
|
android:numeric |
如果被设置,该TextView有一个数字输入法。 此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。 |
|
android:password |
以小点”.”显示文本 |
|
android:phoneNumber |
设置为电话号码的输入方式。 |
|
android:privateImeOptions |
设置输入法选项,此处无用,在EditText将进一步讨论。 |
|
android:scrollHorizontally |
设置文本超出TextView的宽度的情况下,是否出现横拉条。 |
|
android:selectAllOnFocus |
如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。 TextView中设置后无效果。 |
|
android:shadowColor |
指定文本阴影的颜色,需要与shadowRadius一起使用。 |
|
android:shadowDx |
设置阴影横向坐标开始位置。 |
|
android:shadowDy |
设置阴影纵向坐标开始位置。 |
|
android:shadowRadius |
设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。 |
|
android:singleLine |
设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。 如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。 如果不设置singleLine或者设置为false,文本将自动换行 |
|
android:text |
设置显示文本. |
|
android:textAppearance |
设置文字外观。如“?android:attr/textAppearanceLargeInverse ”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。 可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/ textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/ textAppearanceSmall/textAppearanceSmallInverse |
|
android:textColor |
设置文本颜色 |
|
android:textColorHighlight |
被选中文字的底色,默认为蓝色 |
|
android:textColorHint |
设置提示信息文字的颜色,默认为灰色。与hint一起使用。 |
|
android:textColorLink |
文字链接的颜色. |
|
android:textScaleX |
设置文字缩放,默认为1.0f。分别设置0.5f/1.0f/1.5f/2.0f |
|
android:textSize |
设置文字大小,推荐度量单位”sp”,如”15sp” |
|
android:textStyle |
设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开 |
|
android:typeface |
设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3] |
|
android:height |
设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) |
|
android:maxHeight |
设置文本区域的最大高度 |
|
android:minHeight |
设置文本区域的最小高度 |
|
android:width |
设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米) |
|
android:maxWidth |
设置文本区域的最大宽度 |
|
android:minWidth |
设置文本区域的最小宽度 |
Android笔记(十三) Android中的基本组件——文本的更多相关文章
- Android 笔记之 Android 系统架构
Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...
- android 49 广播接收者中启动其他组件
main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- Android笔记: Android版本号
由于有2套版本号 总是对应不准 记下来做过标记 Android 4.3 ----18 Android 4.2---17 Android 4.1---16 Android 4.0.3---15Andro ...
- Android笔记:java 中的数组
在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struc ...
- Android笔记:java 中的枚举
部分数据使用枚举比较方便,java中的enmu不如c#中使用方便 记录备忘 以c#中的代码为例 public enum PlayState { /// <summary> /// 关闭 / ...
- Android笔记之Fragment中创建ViewModel的正确方式
之前一直都是这么写的 pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class); //参数this是当前fragment ...
- Java学习笔记十三:Java中的类和对象
Java中的类和对象 一:什么是对象: 总的来说就是"万物皆对象",客观存在的事物皆为对象.是计算机所关注的具体信息. 对象(object)是一件事.一个物体.一个名词,或可以获得 ...
- Android笔记:android的适配
public int Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayM ...
- Android Fragment中使用Intent组件拍照
要在activity里面去接受,然后传递给fragment对象,fragment有很多回调调用不到 你的设备有摄像头吗? 为了确保市场上的大多数设备都能运行你的程序,必须在项目中做一些检测,保证使用的 ...
随机推荐
- 宣化上人: 大佛顶首楞严经四种清净明诲浅释(8-9)(转自学佛网:http://www.xuefo.net/nr/article23/230825.html)
大佛顶首楞严经四种清净明诲浅释(8) 唐天竺·沙门般剌密帝译 宣化上人主讲 一九八三年四月十七日晚讲于万佛圣城 各自谓己得上人法.詃惑无识.恐令失心.所过之处.其家耗散. 各自谓己:每一个都是自己称赞 ...
- 动态调用webservice时 ServiceDescriptionImporter类在vs2010无法引用的解决方法 (转)
本文转自:http://blog.csdn.net/limlimlim/article/details/8647038 [导读]ServiceDescriptionImporter是创建Web Ser ...
- SignalR 传Model类型的参数
目录 集线器方法 js调用 集线器方法 集线器写了一个方法是这样的 public void test(string name, Customer customer) 第一个参数是string类型的,第 ...
- asp.net Forms身份验证详解
在做网站的时候,都会用到用户登录的功能.对于一些敏感的资源,我们只希望被授权的用户才能够访问,这让然需要用户的身份验证.对于初学者,通常将用户登录信息存放在Session中,笔者在刚接触到asp.ne ...
- 【Leetcode_easy】896. Monotonic Array
problem 896. Monotonic Array solution1: class Solution { public: bool isMonotonic(vector<int>& ...
- vue项目开发优化
1 按需引入ui组件 比如elementUI,不要直接在main.js中全局引入所有的组件,可以根据elementui的说明文件,按需引入 项目中的组件 2 异步引入路由组件 使用 { path:'/ ...
- python:动态参数*args
动态参数 顾名思义,动态参数就是传入的参数的个数是动态的,可以是1个.2个到任意个,还可以是0个.在不需要的时候,你完全可以忽略动态函数,不用给它传递任何值. Python的动态参数有两种,分别是*a ...
- js控制数量包含截取
<div class="usermes_index_line"> 进行中的单 <div id="usermes_index_line_i2"& ...
- CentOS 7命令行安装GNOME桌面
1.切换至root用户,检查一下已经安装的软件以及可以安装的软件 [root@localhost ~]# yum grouplistLoaded plugins: fastestmirrorThere ...
- [转帖]14-使用glusterfs做持久化存储
14-使用glusterfs做持久化存储 https://www.cnblogs.com/guigujun/p/8366558.html 使用glusterfs做持久化存储 我们复用kubernete ...