Android中TextView详解

来源:互联网 发布:js实现tab选项卡切换 编辑:程序博客网 时间:2024/06/05 03:44
public class

TextView

extends View

implements ViewTreeObserver.OnPreDrawListener

java.lang.Object   ↳android.view.View    ↳android.widget.TextViewKnown Direct Subclasses

Known Indirect Subclasses

Class Overview


Displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; see EditText for a subclass that configures the text view for editing.

XML attributes

See TextView AttributesView Attributes

http://developer.android.com/reference/android/widget/TextView.html

控制不同的文字字体

TextView对象里有许多与字形相关的方法,如使用setTextSize来改变字体大小、使用setTypeface来指定文字字体。这里主要解说通过外部资源assets,引用外部的字体文件(The Type Font),再通过Typeface类的createFromAsset方法,让TextView可通过setTypeface来顺利改变字体。

/*必须事先在assets底下创建一fonts文件夹

*并放入要使用的字体文件(.ttf)

*并提供相对路径给createFromAsset()来创建Typeface对象 */

        AssetManager mgr = getAssets();
        Typeface tf = Typeface.createFromAsset(mgr, "fonts/digifaw.ttf");
        myText.setTypeface(tf);

或者    myText.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/digifaw.ttf"));

assets/fonts/digifaw.ttf

    将外部字体文件放在fonts/的底下,就可以通过AssetsManage来引用外部资源。此处需要主要的是,字体文件必须符合True Type Font格式。否则,即便程序编译时不出错,在运行时也会发生无法更改字体的情况。

    上述程序使用了Typeface类,并使用外部字体文件来改变文字的字体。不过,目前Android的版本似乎在支持True Type字体方面有些问题,即便是用了不支持的字体,Android也不会发出错误信息,而是以Droid Sans默认字体替换,所以当遇到使用外部字体却又发现字体没有变化时,原因就处在Android没有支持这个字体,而非程序本身发生错误。

    除了通过外部资源来构造Typeface外,也可以通过defaultFromStyle来使用Android内置的几款Typeface.

常数名称及概要:

int BOLD 1 ; int BOLD_ITALIC 3;  Typeface DEFAULT;  Typeface DEFAULT_BOLD;  int ITALIC 2;  

Typeface MONOSPACE;  int NORMAL 0;  Typeface SANS_SERIF;  Typeface SERIF.

属性中:android:typeface="bold"   代码中:myText.setTypeface(typeface);

android:typeface        设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]

    除了将字体文件放入手机当中,也可以将字体以import的方式导入项目资源,方法如下:

Resources.StyledAttributes resFont =getContext().obtainStyledAttributes(attrs,R.styleable.UnicodeTextView);

String fontName=resFont.getString(R.styleable.UnicodeTextView_font);

if(fontName!=null){/*在这里处理更改字体的程序片段*/}

android:textStyle       设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开

控制文字字体大小

代码中:myText.setTextSize(22);

属性中:android;textSize="22sp"

android:textScaleX设置文字之间间隔,默认为1.0f

改变文字颜色

属性中:android:textColor="#ffffff"

代码中:setTextColor(int);

android:textColorHighlight  被选中文字的底色,默认为蓝色
android:textColorHint       设置提示信息文字的颜色,默认为灰色。与hint一起使用。

android:includeFontPadding  设置文本是否包含顶部和底部额外空白,默认为true。

android:textAppearance      设置文字外观。如 “?android:attr/textAppearanceLargeInverse”这里引用的是android系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可以有以下几种选择textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse

其他属性

①android:drawableBottom    在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。
②android:drawableLeft      在text的左边输出一个drawable,如图片。

③android:drawableRight     在text的右边输出一个drawable。

④android:drawableTop       在text的正上方输出一个drawable
⑤android:drawablePadding   设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。


android:ellipsize           设置当文字过长时,该控件该如何显示。有如下值设置:”start”—-省略号显示在开头;”end” ——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)。

android:singleLine          设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:singleLine="true" android:layout_width="20dp"将只示部分。如果不设置singleLine或者设置为false,文本将自动换行。

android:scrollHorizontally  设置文本超出TextView的宽度的情况下,是否出现横拉条。

<TextView
        android:id="@+id/mytv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="温馨提示:我在练习跑马灯效果,一起学习吧!2013年2月21日我写下了这句话,共勉之"
        />


android:height              设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
android:maxHeight           设置文本区域的最大高度
android:minHeight           设置文本区域的最小高度
android:width               设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
android:maxWidth            设置文本区域的最大宽度
android:minWidth            设置文本区域的最小宽度


android:autoLink            设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)

android:linksClickable      设置链接是否点击连接,即使设置了autoLink。

android:textColorLink       文字链接的颜色.


android:shadowColor         指定文本阴影的颜色,需要与shadowRadius一起使用。
android:shadowDx            设置阴影横向坐标开始位置。
android:shadowDy            设置阴影纵向坐标开始位置。
android:shadowRadius        设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。


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:lineSpacingExtra  设置行间距。
android:lineSpacingMultiplier设置行间距的倍数。如”1.2”


android:gravity           设置文本位置,如设置成“center”,文本将居中显示。

原创粉丝点击