android用来显示界面的组件,Android 自学之基本界面组件(上)

文本款(TextView)和编辑框(EditText)的功能和用法

TextView直接继承了View,他还是EditText、Button两个UI组件的父类,TextView的作用就是在界面上显示文字(相当于Java中AWT中标签[JLabel],但有比他强大些)。

TextView类及其子类的类图如图所示:

d8c613bb081c42c7f5551e3c818d549a.png

TextView的XML属性及相关方法和说明:

XML属性

相关方法

说明

android:autoLink

setAutoLinkMask(int)

是否符合指定格式的文本转换为可单击的超链接形式

android:cursorVisible

setCursorVisible(boolean)

设置该文本框的光标是否可见

android:drawableBottom

setCompounDrawablesWithIntrinsicBounds

(Drawable,Drawable,Drawable,Drawable)

在文本框内文本的底端绘制指定图像

android:drawableLeft

setCompounDrawablesWithIntrinsicBounds

(Drawable,Drawable,Drawable,Drawable)

在文本框内文本的左边绘制指定图像

android:drawablePadding

setCompounDrawablesWithIntrinsicBounds

(Drawable,Drawable,Drawable,Drawable)

设置文本框内文本与图形之间的间距

android:drawableRight

setCompounDrawablesWithIntrinsicBounds

(Drawable,Drawable,Drawable,Drawable)

在文本框内文本的右边绘制指定图像

android:drawableTop

setCompounDrawablesWithIntrinsicBounds

(Drawable,Drawable,Drawable,Drawable)

在文本框内文本的顶端绘制指定图像

android:editable

设置该文本是否允许编辑

android:ellipsize

设置当显示的文本超过TextView的长度是如何处理文本内容

android:gracity

setGravity(int)

设置文本框内文本的对齐方式

android:height

setHeight(int)

设置该文本框的高度(以pixel为单位)

android:hint

setHint(int)

设置当该文本框内容为空时,文本框内默认显示的提示文本

android:minHeight

setMinHeight(int)

设置该文本框的最小高度(以pixel为单位)

android:maxHeight

setMaxHeight(int)

设置该文本框的最大高度(以pixel为单位)

android:minWidth

setMinWidth(int)

设置该文本框的最小宽度(以pixel为单位)

android:maxWidth

setMaxWidth(int)

设置该文本框的最大宽度(以pixel为单位)

android:lines

setLines(int)

设置该文本框默认占几行

android:MiLines

setMiLines(int)

设置该文本框最少占几行

android:MaxLines

setMaxLines(int)

设置该文本框最多占几行

android:password

setTransformationMethod(TransfirmationMethod)

设置该文本框是一个密码框(以点代替字符)

android:phoneNumber

setKevListener(KeyListener)

设置该文本框只能接受电话号码

android:scrollHorizontally

setHorizontallyScorlling(boolean)

设置当该文本框不够显示全部内容时是否允许水平滚动

android:selectAllOnFocus

setSelectAllOnFocus(boolean)

如果文本框的内容可选择,设置当它获得焦点时是否自动选中所有文本

android:singleLine

setTransformationMethod

设置该文本框是否为单行模式。如果设为true,文本框不会换行

android:shadowColor

setShadowLayer(float,float,float,int)

设置文本框内文本的阴影的颜色

android:shadowDx

setShadowLayer(float,float,float,int)

设置文本框内文本的阴影在水平方向的偏移

android:shadowDy

setShadowLayer(float,float,float,int)

设置文本框内文本的阴影在垂直方向的偏移

android:shadowRadius

setShadowLayer(float,float,float,int)

设置文本框内文本的阴影的角度

android:text

setText(CharSequence)

设置文本框内文本的内容

android:textColor

setTextColor(ColorStateList)

设置该文本框内文本的颜色

android:tetColorHighlight

setHighlightColor(int)

设置该文本框内文本被选中时的颜色

android:textScaleX

setTextScaleX(float)

设置该文本框内文本水平方向上的缩放因子

android:textSize

setTextSize(float)

设置该文本框内文本的字体大小

android:textStyle

setTypeface(Typeface)

设置该文本框内文本的字体风格,如粗体,斜体

android:typeface

setTypeface(Typeface)

设置该文本框内文本的字体

android:width

setWidth(int)

设置该文本框的宽度

上表中android:autoLink属性值是如下几个属性值的一个或几个,多个属性值之间用竖线隔开:

none:不设置任何超链接

web(对应于Linkify.WEB_URLS):将文本中的URL地址转换为超链接

email(对应于Linkify.EMAIL_ADDRESSES):将文本中的E-mail地址转换为超链接

phone(对应于Linkify.PHONE_NUMBERS):将文本中的电话号码转换为超练接

map(对应于Linkify.MAP_ADDRESSES):将文本中的接到地址转换为超链接

all:相当于指定weblemaillphonelmap

上表中android:ellipsize属性可支持如下几个属性值。

none:不进行任何处理

start:在文本开头部分进行省略

middle:在文本中间部分进行省略

end:在文本结尾出进行省略

marquee:在文本结尾出以淡出的方式省略

下面我们用例子来例举上表中的一些属性:不同字体、不同颜色的文本、URL

layout/main.xml

fcecaa27ea5212ceb9bf034c36bfbf34.gif1

2 xmlns:tools="http://schemas.android.com/tools"

3 android:orientation="vertical"

4 android:layout_width="fill_parent"

5 android:layout_height="fill_parent">

6

7

8

10 android:layout_height="wrap_content"

11 android:text="我爱Java"

12 android:textSize="20pt"

13 />

14

15

17 android:layout_height="wrap_content"

18 android:singleLine="true"

19 android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我aaaJava"

20 android:ellipsize="middle"

21 />

22

23

25 android:layout_height="wrap_content"

26 android:singleLine="true"

27 android:text28 android:autoLink="email"

29 />

30

31

33 android:layout_height="wrap_content"

34 android:text="测试文字"

35 android:shadowColor="#0000ff"

36 android:shadowDx="15.0"

37 android:shadowDy="20.0"

38 android:shadowRadius="45.0"

39 android:textColor="#ff0000"

40 android:textSize="25pt"

41 />

42

43

44 android:layout_width="fill_parent"

45 android:layout_height="wrap_content"

46 android:text="@string/hello"

47 android:password="true"

48 />

49

50

fcecaa27ea5212ceb9bf034c36bfbf34.gif

所展现的效果图如下:

d94354bd28a26229e259ba7359e0e6fe.png

范例:带边框、图片的TextView

fcecaa27ea5212ceb9bf034c36bfbf34.gif1

2 xmlns:tools="http://schemas.android.com/tools"

3 android:orientation="vertical"

4 android:layout_width="fill_parent"

5 android:layout_height="fill_parent">

6

7

8

10 android:layout_height="wrap_content"

11 android:text="带边框的文本"

12 android:background="@drawable/bg_border"

13 android:textColor="@color/abc_search_url_text_holo"

14 />

15

16

17

19 android:layout_height="wrap_content"

20 android:text="带图片的文本"

21 android:drawableLeft="@drawable/ic_launcher"

22 />

23

24

fcecaa27ea5212ceb9bf034c36bfbf34.gif

效果图:

7e69f9a94cca557afa615ea08925f091.png

范例:一个注册界面

fcecaa27ea5212ceb9bf034c36bfbf34.gif1

2 xmlns:tools="http://schemas.android.com/tools"

3 android:orientation="vertical"

4 android:layout_width="fill_parent"

5 android:layout_height="fill_parent">

6

7

9 android:layout_height="wrap_content"

10 android:text="用户名:"

11 android:textSize="10sp"

12 android:background="@drawable/bg_border"

13 />

14

16 android:layout_height="wrap_content"

17 android:hint="请填写登录帐号"

18 android:selectAllOnFocus="true"

19 />

20

21

22

24 android:layout_height="wrap_content"

25 android:text="密码:"

26 android:textSize="10pt"

27 android:background="@drawable/bg_border"

28 />

29

31 android:layout_height="wrap_content"

32 android:password="true"

33 />

34

35

36

38 android:layout_height="wrap_content"

39 android:text="电话号码:"

40 android:textSize="10pt"

41 android:background="@drawable/bg_border"

42 />

43

45 android:layout_height="wrap_content"

46 android:hint="请填写您的电话号码"

47 android:selectAllOnFocus="true"

48 android:phoneNumber="true"

49 />

50

51

53 android:layout_height="wrap_content"

54 android:text="注册"

55 />

56

57

58

fcecaa27ea5212ceb9bf034c36bfbf34.gif

上面代码里面的TextView是设置了背景图片的,所以在展示的效果图上你们呢会看到三个黑的框框,为什么三个框框的会有一个比另外两个小,那是因为我设置的宽度单位不一样(用户名的宽度是sp,其他的是用的pt);所展示的效果,效果图如下:

4a8d100609b9f912181046fa0cf70f44.png

原文:http://www.cnblogs.com/Yang-jing/p/3747469.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值