在java matcher find代码中 TextView textView = (TextView) findViewById(R.id.textView); textView.setText("

我怎样才能旋转 Android
与背景 color
即不交错 (需要抗锯齿)
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
我有一个静态 xml layout 带有背景 color (看起来像一个打印的标签) 希望以显示在一个角度。 的问题是边缘的背景看,像他们被呈现在 1980 年代 (锯齿状-不消除锯齿)。
怎样才能 textview ,看起来像一个角的标签,就看起来体面 (即抗锯齿)?
&LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:paddingRight="5dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:orientation="vertical"&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:maxLines="1"
android:singleLine="true"
android:text="Sample Text"
android:textAllCaps="true"
android:textColor="#ffffff"
android:textSize="12sp"
android:background="#3db8eb"
android:rotation="-3"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:paddingLeft="3dp"
android:paddingRight="3dp"/&
&/LinearLayout&
它的外观:
解决方法 1:
尝试一些不同的东西之后, 我终于找到工作虽然有点令人费解的东西。 这里是如何得到平滑的边缘上旋转 textview 固体 color 背景:
步骤 1: 创建 drawable 资源与 color (即 ColorDrawable 或 Shape 喜欢一个矩形)
&?xml version="1.0" encoding="utf-8"?&
&shape xmlns:android="/apk/res/android" android:shape="rectangle"&
&solid android:color="@color/blue" /&
步骤 2: 将背景设置为程序 drawable 。
android:id="@+id/my_textview"
android:background="@drawable/blue_rectangle" /&
步骤 3: 在代码中调用:
TextView textView = (TextView)findViewById(R.id.my_textview);
textView.getBackground().setFilterBitmap(true);Android编程开发之TextView文字显示和修改方法(附TextView属性介绍)
作者:贺臣
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Android编程开发之TextView文字显示和修改方法,结合实例详细分析了Android中TextView控件关于文字的显示及修改技巧,并附带了TextView属性介绍,需要的朋友可以参考下
本文实例讲述了Android编程开发之TextView文字显示和修改方法。分享给大家供大家参考,具体如下:
一. 新建一个Activity 和 Layout
首先在layout文件夹中新建一个activity_main.xml,在新建工程的时候一般默认会新建此xml文件,修改其代码如下:
activity_main.xml 代码
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" &
android:id="@+id/lblTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="88dp"
android:layout_marginTop="51dp"
android:text="TextView" /&
&/RelativeLayout&
修改MainActivity.java文件代码如下:
MainActivity.java 代码:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView lblTitle=(TextView)findViewById(R.id.lblTitle);
lblTitle.setText("这是显示的内容");
通过以上方法就可以修改TextView中的显示文字内容了
二. 显示连接文字
显示连接字符串
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView lblTitle=(TextView)findViewById(R.id.lblTitle);
lblTitle.setText("&a href=\"\"&百度&/&");
修改如上代码,然后运行查看手机界面,发现并没有自动以Html格式来解析此字符串,说明TextView模式是不支持Html字符串解析的
解析带有http的文字
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView lblTitle=(TextView)findViewById(R.id.lblTitle);
lblTitle.setAutoLinkMask(Linkify.ALL);
lblTitle.setText("&a href=\"\"&百度&/&");
我们可以通过setAutoLinkMask 来设置带有连接的字符串,或者使用如下代码:
连接文字显示:
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" &
android:id="@+id/lblTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="88dp"
android:layout_marginTop="51dp"
android:autoLink="all"
android:text="TextView" /&
&/RelativeLayout&
附:Android的TextView属性XML详解:
属性名称&描述
android:autoLink&设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)
android:autoText&如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。
android:bufferType&指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符,
也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,参见这里1、这里2。
android:capitalize&设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。
android:cursorVisible&设定光标为显示/隐藏,默认显示。
android:digits&设置允许输入哪些字符。如“.+-*/%\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。在EditView再做说明,可以先看这篇帖子:这里。
android:imeActionLabel&设置IME动作标签。在EditView再做说明。
android:includeFontPadding&设置文本是否包含顶部和底部额外空白,默认为true。
android:inputMethod&为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。
android:inputType&设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。
android:marqueeRepeatLimit&在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
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(毫米),与layout_width的区别看这里。
android:maxWidth&设置文本区域的最大宽度
android:minWidth&设置文本区域的最小宽度
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具是为设计的一个图标集合字体,里面包含了300多个常用图标。使用Font-Awesome还具有如下优点:
1. 减少了图标的绘制工作
2. 可以设置图标的颜色和大小
3. 减少了图标的大小并且可以减少apk的大小,只需要一个图标字体文件即可,不需要各种尺寸的图标文件了,比如 HDPI、XHDPI等各种尺寸的图标。
Font-Awesome的使用方式
到Font-Awesome主页下载Font-Awesome字体(fontawesome-webfont.ttf)文件并放到项目的assets目录下,找到需要用的图标对应的字符串( 包含了一份图标和字符串的对应文件,最新的对应关系在下载的Font-Awesome字体中的css目录中的font-awesome.css文件中查找),在TextView中设置需要使用的图标文字,然后设置TextView的字体为自定义的Font-Awesome字体。
//在XML中使用
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/icon_credit_card"
android:textSize="50sp"
android:textColor="#F59012"
android:textAppearance="?android:attr/textAppearanceLarge" /&
//或者在代码中使用:
myTextView.setText(getString(R.string.icon_credit_card));
//然后设置自定义字体:
TextView txt = (TextView) findViewById(R.id.textView1);
Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
txt.setTypeface(font);
1234567891011121314151617181920
//在XML中使用&TextView&&&&android:id="@+id/textView1"&&&&android:layout_width="wrap_content"&&&&android:layout_height="wrap_content"&&&&android:text="@string/icon_credit_card"&&&&android:textSize="50sp"&&&&android:textColor="#F59012"&&&&android:textAppearance="?android:attr/textAppearanceLarge" /&//或者在代码中使用:&myTextView.setText(getString(R.string.icon_credit_card));&&&//然后设置自定义字体:&&&&&TextView txt = (TextView) findViewById(R.id.textView1);&&&&&&Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");&&&&txt.setTypeface(font);
另外如果需要在使用Drawable的地方使用Font-Awesome图标,则可以自定义一个Drawable,然后在代码中使用该Drawable,详细使用方式请参考fonticon这个示例项目:
另外除了Font-Awesome图标字体以为,还有其他的图标字体,例如
本文出自 云在千峰,转载时请注明出处及相应链接。本文永久链接: /?p=540
Ɣ回顶部  开门见山,这一篇博客主要讲一下在Android开发中,UI控件TextView的一些使用方式,并且通过四个例子实现一般项目中需要的效果来讲解TextView的使用。并且在之后的一段时间之内,都会讲解关于AndroidUI控件的开发。
  之前讲解Android布局的时候,就已经说明,所有Layout都是View的子类或者间接子类。而TextView也一样,是View的直接子类。它是一个文本显示控件,提供了基本的显示文本的功能,并且是大部分UI控件的父类,因为大部分UI控件都需要展示信息。
  如果仅仅是展示文本,那么TextView的作用就太小了,所以它还预定义了一些类似于HTML的标签,通过这些标签可以使TextView控件显示不同的颜色、大小、字体、图片、链接。这些HTML标签都需要android.text.Html类的支持,但是并不包括所有的HTML标签。
  常用的可以再TextView中设定的标签有:
&font&:设置颜色和字体。
&big&:设置字体大号
&small&:设置字体小号
&i&\&b&:斜体\粗体
&a&:连接网址
&img&:图片
  使用这些标签可以用Html.fromHtml方法将这些标签的字符串转换成CharSequence接口,然后在TextView.setText()中进行设置。如果需要响应设置的HTML标签进行响应,需要设置TextView.setMovementMethod(LinkMovementMethod.getInstance())。
  CharSequence为接口类型,大家可能对其有点陌生,但是它的子类肯定会让大家有熟悉的感觉,String、StringBuffer、StringBuilder、SpannableString、SpannableStringBuilder都是其子类,它包括了字符串的所有类,因为面向对象的多态性,在这里把他理解成字符串类的抽象即可。
  除了使用HTML标签的方式设定显示文本中的URL地址、邮箱地址、电话等产生超链接出发相应的服务,可以使用android:autoLink属性来设置,以下是android:autoLink属性的介绍:
None:默认的,不匹配任何连接。
web:网址。
email:邮箱。
phone:电话号码。
map:匹配映射网址。
all:匹配所有连接。
显示富文本
  讲了这么多,通过第一个例子来讲解一下TextView使用HTML标签设定样式和通过autoLink属性来设置超链接效果,在XML布局文件中定义两个TextView,分别展示HTML标签和autoLink属性的使用。
  XML布局文件textviewdemo.xml代码:
1 &?xml version="1.0" encoding="utf-8"?&
2 &LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:autoLink="all"
android:textSize="20sp"
20 &/LinearLayout&
  Activity文件textViewDemoActivity.java代码:
1 package cn.bgxt.
3 import android.app.A
4 import android.os.B
5 import android.text.H
6 import android.text.method.LinkMovementM
7 import android.widget.TextV
9 public class textViewDemoActivity extends Activity {
private TextView textView1,textView2;
public textViewDemoActivity() {
// TODO Auto-generated constructor stub
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.textviewdemo);
//通过Id获得两个TextView控件
textView1=(TextView)findViewById(R.id.textView1);
textView2=(TextView)findViewById(R.id.textView2);
//设置需要显示的字符串
String html="&font color ='red'&Hello android&/font&&br/&";
html+="&font color='#0000ff'&&big&&i&Hello android&/i&&/big&&/font&&p&";
html+="&big&&a href=''&百度&/a&&/big&";
//使用Html.fromHtml,把含HTML标签的字符串转换成可显示的文本样式
CharSequence charSequence=Html.fromHtml(html);
//通过setText给TextView赋值
textView1.setText(charSequence);
//设定一个点击的响应
textView1.setMovementMethod(LinkMovementMethod.getInstance());
String text="我的URL:/plokmju/\n";
text+="我的email:\n";
text+="我的电话:+86 010-";
//因为textView2中有autoLink=&all&的属性设定,所以会自动识别对应的连接,点击出发对应的Android程序
textView2.setText(text);
 显示效果:
TextView显示图片
  第二个例子讲解一下在TextView中显示图片的例子,依然是使用HTML标签的方式定义样式,但是使用的是Html.fromHtml()的另外一个重载的静态方法,可以设定&img&标签中的图像资源。
  static Spanned fromHtml(String source,Html.ImageGetter imageGetter,Html.TagHandler tagHandler)
  对于这个方法,在imageGetter参数中设定&img&标签中的图像资源文件,tagHandler主要是为了处理Html类无法识别的html标签的情况,一般不会用上,传值为null即可。
  布局XML文件textvideforimg.xml代码:&
1 &?xml version="1.0" encoding="utf-8"?&
2 &LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
android:id="@+id/textImg"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" /&
11 &/LinearLayout&
  Activity文件textviewForImgActivity.java代码:
1 package cn.bgxt.
3 import java.lang.reflect.F
5 import android.R.
6 import android.app.A
7 import android.graphics.drawable.D
8 import android.os.B
9 import android.text.H
10 import android.text.Html.ImageG
11 import android.text.method.LinkMovementM
12 import android.widget.TextV
14 public class textviewForImgActivity extends Activity
private TextView textViewI
public textviewForImgActivity() {
// TODO Auto-generated constructor stub
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.textvideforimg);
textViewImg=(TextView)findViewById(R.id.textImg);
textViewImg.setTextColor(color.white);
textViewImg.setBackgroundColor(color.black);
textViewImg.setTextSize(20);
//设定HTML标签样式,图片3为一个超链接标签a
String html="图像1&img src='image1'/&图像2&img src='image2'/&\n";
html+="图像3&a href=''&&img src='image3'/&&/a&";
//fromHtml中ImageGetter选择html中&img&的图片资源
CharSequence cs=Html.fromHtml(html, new ImageGetter() {
public Drawable getDrawable(String source) {
//source为html字符串中定义的&img&中的src的内容
//返回值Drawable就是对应的&img&显示的图片资源
Drawable draw=null;
if(source.equals("image1"))
draw=getResources().getDrawable(R.drawable.image1);
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
else if(source.equals("image2"))
//设定image2尺寸等比缩小
draw=getResources().getDrawable(R.drawable.image2);
draw.setBounds(0, 0, draw.getIntrinsicWidth()/2, draw.getIntrinsicHeight()/2);
//使用反射会更简便,无需知道src与资源Id的对应关系
draw=getResources().getDrawable(getResourceId(source));
draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight());
textViewImg.setText(cs);
textViewImg.setMovementMethod(LinkMovementMethod.getInstance());
public int getResourceId(String source)
//使用反射机制,通过属性名称,得到其内的值
Field field=R.drawable.class.getField(source);
return Integer.parseInt(field.get(null).toString());
} catch (Exception e) {
// TODO: handle exception
  效果截图,其中第三个图片点击会触发浏览器访问百度网址:
在TextView增加Click事件
  第三个例子在TextView添加点击事件,导航到其他的Activity中。使用SpannableString.setSpan()设定那一段文本需要相应点击事件。与之类似的还有SpannableBuilder对象,他们的关系和String与StringBuilder一样。
  void setSpan(Object what,int start,int end,int flags)
  在what参数中传递一个抽象类ClickableSpan,需要实现其onClick()方法,此为指定文本的点击相应时间。start和end分别指定需要响应onClick()方法的文本开始与结束。flags设定一个标识,确定使用什么方式选择文本块,一般使用Spanned接口下的SPAN_EXCLUSIVE_EXCLUSIVE对其进行赋值,表示遵循设定的开始于结束位置的文本块。
  布局文件activityclick.xml的代码:
1 &?xml version="1.0" encoding="utf-8"?&
2 &LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
android:id="@+id/clickTextView1"
android:textSize="30dp"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" /&
android:textSize="30dp"
android:layout_marginTop="30dp"
android:id="@+id/clickTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" /&
18 &/LinearLayout&
  Activity文件TextViewOnClickActivity.java的代码:
1 package cn.bgxt.
3 import android.app.A
4 import android.content.I
5 import android.os.B
6 import android.text.SpannableS
7 import android.text.S
8 import android.text.method.LinkMovementM
9 import android.text.style.ClickableS
10 import android.view.V
11 import android.widget.TextV
13 public class TextViewOnClickActivity extends Activity {
private TextView clickTextView1,clickTextView2;
public TextViewOnClickActivity() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityclick);
clickTextView1=(TextView)this.findViewById(R.id.clickTextView1);
clickTextView2=(TextView)this.findViewById(R.id.clickTextView2);
String text1="显示Activity1";
String text2="显示Activity2";
//使用SpannableString存放字符串
SpannableString spannableString=new SpannableString(text1);
SpannableString spannableString2=new SpannableString(text2);
//通过setSpan设定文本块响应的点击事件
//此处只设定文本的索引为2开始的文本块:Activity1
spannableString.setSpan(new ClickableSpan() {
public void onClick(View widget) {
//导航到一个新的 Activity1中
Intent intent=new Intent(TextViewOnClickActivity.this,Activity1.class);
startActivity(intent);
}, 2, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString2.setSpan(new ClickableSpan() {
public void onClick(View widget) {
// TODO Auto-generated method stub
Intent intent=new Intent(TextViewOnClickActivity.this,Activity2.class);
startActivity(intent);
}, 2, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//对TextView文本进行赋值
clickTextView1.setText(spannableString);
//设置点击响应
clickTextView1.setMovementMethod(LinkMovementMethod.getInstance());
clickTextView2.setText(spannableString2);
clickTextView2.setMovementMethod(LinkMovementMethod.getInstance());
  效果图,从图中可以看出只有点击setSpan中设定的代码块才可以触发点击事件:
跑马灯效果
  说到文本显示,最常见的效果就是跑马灯效果,这里以一个例子展示跑马灯的效果,基本无需使用Java代码,在布局文件中设定各项属性就已经可以实现这个效果了。
  在看代码前,先讲解一下等下会碰到的属性:
android:elipsize:&如果文本长度大于TextView的显示长度,则隐藏那一部分,可赋值为:none(不隐藏)、start(隐藏开始)、middle(隐藏中间)、end(隐藏结束)、marquee(滚动效果)。
android:marqueRepeatLimit:设定需要重复动画的次数,传递一个int值,-1为无限循环。
android:focusable:是否允许获得焦点,传递一个bool值。
android:focusableInTouchMode:是否在获得焦点时对控件有联系,传递一个bool值。
  介绍属性后,直接看代码吧,XML布局文件runlamp_layout.xml代码:
1 &?xml version="1.0" encoding="utf-8"?&
2 &LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" &
android:id="@+id/tvRunLamp" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:background="#FFF"
android:textColor="#000"
android:textSize="20dp"
android:layout_margin="10dp"
android:padding="10dp"/&
20 &/LinearLayout&
  Activity文件RunLampActivity.java代码:
1 package cn.bgxt.
3 import android.app.A
4 import android.os.B
5 import android.text.H
6 import android.text.method.LinkMovementM
7 import android.widget.TextV
9 public class RunLampActivity extends Activity {
private TextView tvRunL
public RunLampActivity() {
// TODO Auto-generated constructor stub
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.runlamp_layout);
tvRunLamp = (TextView) findViewById(R.id.tvRunLamp);
String html = "之前讲解Android布局的时候,就已经说明,所有&a href='/plokmju/p/androidUI_Layout.html'&Layout&/a&都是View的子类或者间接子类。而TextView也一样,是View的直接子类。它是一个文本显示控件,提供了基本的显示文本的功能,并且是大部分UI控件的父类,因为大部分UI控件都需要展示信息。";
CharSequence cs = Html.fromHtml(html);
tvRunLamp.setText(cs);
//因为文本中设定了一个&a&标签,这里设置响应。
tvRunLamp.setMovementMethod(LinkMovementMethod.getInstance());
  运行效果图,是一个滚动的效果:
  在此就说明了Android中TextView,并且以例子的方式说明了一些常用效果的实现。因为TextView是大部分UI控件的父类,所以其内的一些属性对于其他UI控件都是通用的,可以有借鉴的地方。
  请支持原创,尊重原创,转载请注明出处。谢谢。
阅读(...) 评论()

我要回帖

更多关于 java find 的文章

 

随机推荐