Android控件详解之Textview

我将Android控件的Textview的学习知识总结一下和大家共享

在Android新建的第一个工程中,显示的helloworld,就是引用Textview控件。

Textview控件的基本使用方法很假但,在布局文件中使用<Textview>第一既可以了,或者在java代码:Textview textview = (Textview)findViewById(R.id.textview1);

1、Textview的属性设置:

android:textsize设置字体大小,推荐度量单位”sp”, 

android:textcolor设置字体颜色,

android:background设置背景,

android:textColorHighlight被选中文字的底色,默认为蓝色,

android:textColorHint设置提示信息文字的颜色,默认为灰色。与hint一起使用。 android:textColorLink文字链接的颜色,

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(毫米),与layout_width的区别看这里,

android:maxWidth设置文本区域的最大宽度,

android:minWidth设置文本区域的最小宽度

android:textColorHighlight被选中文字的底色,默认为蓝色,

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

android:textColorLink文字链接的颜色.,

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

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

android:autoText如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用,

android:bufferType指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符, 也就是说getText后可调用append方法设置文本内容,

android:capitalize设置英文字母大写类型,

android:cursorVisible设定光标为显示/隐藏,默认显示,

android:digits设置允许输入哪些字符。如“1234567890.+-*/% ()” ,

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设置是否可编辑,

android:editorExtras设置文本的额外的输入数据,

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

android:freezesText设置保存文本的内容以及光标的位置,

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

android:hintText为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在EditView中使用,但是这里也可以用,

android:imeOptions附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号,

android:imeActionId设置IME动作ID,

android:imeActionLabel设置IME动作标签,

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

android:text设置显示文本,

android:singleLine设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示,

android:textAppearance设置文字外观。如“?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的,

android:lines设置文本的行数,设置两行就显示两行,即使第二行没有数据,

android:maxLines设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示,

android:minLines设置文本的最小行数,与lines类似,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的效果比较好。

以上是我整理出来的Textview的属性,可能不是全部,希望对大家有用。

2、Textview显示富文本

接下来我讲讲我对富文本的使用,在Textview类中预定义了一些类似HTML的标签,通过这些标签,可以使Textview控件显示不同的颜色、大小、字体的文字。下面是常用的签:<font> 设置字体和颜色,<big>设置大号字, <small>设置小号字, <i>斜体, <b>粗体, <tt> 等宽字体,<br>换行, <p>换行, <a>链接地址, <img>插入图片。这些标签与html类似,但是不具备HTML标签的全部功能。

在使用这些标签时,不能直接使用在Textview.setText方法设置,需要使用Html.fromHtml方法将这些带标签的字符串转成CharSequence对象,然后在使用setText方法。

下面一个例子:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/textview1" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:padding="20dp" />
	<TextView android:id="@+id/textview2" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:textSize="20sp"
		android:autoLink="all" android:padding="20dp" android:text="@string/link_text" />
</LinearLayout>
java文件实现

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class Main extends Activity
{

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView textView1 = (TextView) findViewById(R.id.textview1);
		TextView textView2 = (TextView) findViewById(R.id.textview2);

		String html = "<font color='red'>I love Android.</font><br>"; 
		html += "<font color='#0000FF' ><big><i>I love Android.</i></big></font><p>";
		html += "<font color='@" + android.R.color.white+ "' ><tt><b><big><u>I love Android.</u></big><b></tt></font><p>";
		html += "<big><a href='http://51happyblog.com'>我的网站:51happyblog.com</a></big>";
		
		
		CharSequence charSequence = Html.fromHtml(html);
		 
		textView1.setText(charSequence);

		textView1.setMovementMethod(LinkMovementMethod.getInstance());
		
		String text = "我的URL:http://51happyblog.com\n";
		text += "我的Email:abcd@126.com\n";
		text += "我的电话:+86 024-12345678";
		textView2.setText(text);
		textView2.setMovementMethod(LinkMovementMethod.getInstance());
	}
}
结果这边就不截图了,大家自己动手试试。

3、在TextView显示图像

这边还是要用到html标签,在这边使用主要是Html.fromHtml()不同参数的使用,该方法带三个参数的复用:public static Spanned fromHtml(String source,ImageGetter imageGetter,TagHandler tagHandler)

source:就是包含Html的字符串。

imageGetter:用于解析<img>标签的图片内容,并封装成Drawable对象。

tagHandler:这个比较不常用,系统处理每一个标签都会去调用TagHandler.handleTag方法。

下面看一个例子:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/textview" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:layout_margin="10dp"
		android:background="#FFF" />
</LinearLayout>

java文件实现:

<span style="background-color: rgb(245, 245, 245);">package mobile.android.text.image;

import java.lang.reflect.Field;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class Main extends Activity
{
	public int getResourceId(String name)
	{
		try
		{  
			Field field = R.drawable.class.getField(name);
			return Integer.parseInt(field.get(null).toString());
		}
		catch (Exception e)
		{

		}
		return 0;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TextView textView = (TextView) findViewById(R.id.textview);
		textView.setTextColor(Color.BLACK);
		textView.setBackgroundColor(Color.WHITE);
		textView.setTextSize(20);
		String html = "图像1<img src='image1'/>图像2<img src='image2'/>图像3<img src='image3'/><p>";
		html += "图像4<a href='http://51happyblog.com'><img src='image4'/></a>图像5<img src='image5'/>";

		</span><span style="background-color: rgb(153, 153, 153);">CharSequence charSequence = Html.fromHtml(html, new ImageGetter()
		{

			@Override
			public Drawable getDrawable(String source)
			{
				Drawable drawable = getResources().getDrawable(
						getResourceId(source));
				
				if (source.equals("image3"))
					drawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2,
							drawable.getIntrinsicHeight() / 2);
				else
					drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
							drawable.getIntrinsicHeight() );
				return drawable; 

			}
		}, null);</span><span style="background-color: rgb(245, 245, 245);">
		
		textView.setText(charSequence);
		textView.setMovementMethod(LinkMovementMethod.getInstance());

	}
}</span>

以上主要的额是阴影部分的代码。

4、TextView点击链接跳转Activity(单机链接实现自己的动作和上面的<a>不一样)

说明一下这边使用的不是<a>这个标签来实现的。引用了SpannableString对象的setSpan的方法。不废话了,直接看代码一目了然:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:id="@+id/textview1" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:textSize="20sp"
		 />
	<TextView android:id="@+id/textview2" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:textSize="20sp"
		 android:layout_marginTop="20dp" />
</LinearLayout>

java文件实现:这边的定义了两个Activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

public class Main extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView textView1 = (TextView) findViewById(R.id.textview1);
		TextView textView2 = (TextView) findViewById(R.id.textview2);

		String text1 = "显示Activity1";
		String text2 = "显示Activity2";

		SpannableString spannableString1 = new SpannableString(text1);
		SpannableString spannableString2 = new SpannableString(text2);

		spannableString1.setSpan(new ClickableSpan()
		{

			@Override
			public void onClick(View widget)
			{
				Intent intent = new Intent(Main.this, Activity1.class);
				startActivity(intent);

			}
		}, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		
		spannableString2.setSpan(new ClickableSpan()
		{

			@Override
			public void onClick(View widget) 
			{
				Intent intent = new Intent(Main.this, Activity2.class);
				startActivity(intent);

			}
		}, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

		textView1.setText(spannableString1);
		textView2.setText(spannableString2);
		
		textView1.setMovementMethod(LinkMovementMethod.getInstance());
		textView2.setMovementMethod(LinkMovementMethod.getInstance());

	}
}

Activity1

public class Activity1 extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setTitle("Activity1");
		
	}

}
Activity2

public class Activity2 extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setTitle("Activity2");
		
	}

}
5、为指定的文本添加背景

比较灵活的指定一个段的字体添加背景

主要代码:

TextView textview = (TextView) findViewById(R.id.textview);	
String text = "<没有背景><黄色背景>\n\n<蓝色背景,红色文字>";	
SpannableString spannableString = new SpannableString(text);	
int start = 6;	
int end = 12;	
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(
Color.YELLOW);	
spannableString.setSpan(backgroundColorSpan, start, end,				
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);		
start = 14;		
ColorSpan colorSpan = new ColorSpan(Color.RED, Color.BLUE);		
spannableString.setSpan(colorSpan, start, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);	
textview.setText(spannableString);
ColorSpan.java:
<pre name="code" class="html">public class ColorSpan extends CharacterStyle
{
	private int mTextColor;
	private int mBackgroundColor;
	public ColorSpan(int textColor, int backgroundColor)
	{
		mTextColor = textColor;
		mBackgroundColor = backgroundColor;
	}
	@Override
	public void updateDrawState(TextPaint tp)
	{
		tp.bgColor = mBackgroundColor;
		tp.setColor(mTextColor);	
	}
}

以上五点是我目前对TextView使用的一些知识总结,希望对大家有所帮助,欢迎大家一起来共同学习。

 









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值