TextView是简单而又复杂的控件,简单是使用上,复杂是源代码研究。基础编程这里只讲是如何使用。

TextView主要分为两种使用方法,一种是xml中不带span的textview,另外一种是Span的textview。

1.因为是在democoderjoy中使用,这里我们新建一个TextviewActi的activity。

public class TextViewActi extends Activity {
@Override
protected void onCreate(Bun。。。。

2.直接上布局不带span的布局TextView的使用。

<TextView
android:id="@+id/txt1One"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Textview demo 大家来学习 啊啊 zcx zcx zcx zcx,呵呵呵呵呵呵呵呵~"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:shadowColor="#F9F900"
android:shadowDx="10.0"
android:shadowDy="10.0"
android:shadowRadius="3.0"
android:text="带阴影的TextView"
android:textColor="#4A4AFF"
android:textSize="18sp" /> <TextView
android:id="@+id/txtOne"
android:layout_width="200dp"
android:layout_height="50dp"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/txt_rectborder"
android:text="矩形边框的TextView" /> <TextView
android:id="@+id/txtTwo"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/txt_radiuborder"
android:text="圆角边框的TextView" /> <TextView
android:id="@+id/zhang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableTop="@mipmap/ic_launcher"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"
android:drawableBottom="@mipmap/ic_launcher"
android:drawablePadding="10dp"
android:text="张全蛋" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="www.baidu.com"
android:autoLink="web"/>
<TextView
android:id="@+id/txtThree"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:textSize="18sp"
android:gravity="center"
/>
<TextView
android:id="@+id/txtFor"
android:layout_width="wrap_content"
android:layout_height="80dp" android:textSize="16sp" />

从上到下布局分别代表是跑马灯效果、带阴影的、矩形边框、圆角边框、四周图片、超链接、(span使用)、文字点击效果(span)。

3.带span的效果采用SpannableStringBuilder和SpannableString这个来使用

参考代码如下

TextView t2 = (TextView) findViewById(R.id.txtThree);

        SpannableString span = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");
//1.设置背景色,setSpan时需要指定的flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)
span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//2.用超链接标记文本
span.setSpan(new URLSpan("tel:4155551212"), 2, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//3.用样式标记文本(斜体)
span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//4.用删除线标记文本
span.setSpan(new StrikethroughSpan(), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//5.用下划线标记文本
span.setSpan(new UnderlineSpan(), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//6.用颜色标记
span.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//7.//获取Drawable资源
Drawable d = getResources().getDrawable(R.drawable.icon);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//8.创建ImageSpan,然后用ImageSpan来替换文本
ImageSpan imgspan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
span.setSpan(imgspan, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
t2.setText(span);
t2.setMovementMethod(LinkMovementMethod.getInstance());

4.还有一个html的TextVIew。参考代码中使用

TextView t1 = (TextView)findViewById(R.id.txtOne);
String s1 = "<font color='blue'><b>百度一下,你就知道~:</b></font><br>";
s1 += "<a href = 'http://www.baidu.com'>百度</a>";
t1.setText(Html.fromHtml(s1));
t1.setMovementMethod(LinkMovementMethod.getInstance());

实验效果如下图:

参考链接

android-基础编程-TextView的更多相关文章

  1. Android 基础一 TextView,Style样式,Activity 传值,选择CheckBox 显示密码

    1.修改TextView字体 mTextView = (TextView) findViewById(R.id.textview1); mTextView.setText("I am her ...

  2. android: 多线程编程基础

    9.1   服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...

  3. Android网络编程基础

    Android网络编程只TCP通信 TCP 服务器端工作的主要步骤如下.步骤1 调用ServerSocket(int port)创建一个ServerSocket,并绑定到指定端口上.步骤2 调用acc ...

  4. Android基础控件TextView

    1.常用属性 <TextView android:id="@+id/text11" //组件id android:layout_width="match_paren ...

  5. Android Studio 之 TextView基础

    •引言 在开始本节内容前,先要介绍下几个单位: dp(dip) : device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关 一般我们为了支持 ...

  6. Android基础之布局ConstraintLayout

    Google I/O 2016 上发布了 ConstraintLayout,据说很强大,那就一探究竟吧! gradle配置 compile 'com.android.support.constrain ...

  7. Android Socket编程

    花了大概两天的时间,终于把Android的Socket编程给整明白了.抽空和大家分享一下: Socket Programming on Android Socket 编程基础知识: 主要分服务器端编程 ...

  8. Qt on Android 核心编程

    Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著   ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...

  9. Mono.Android 基础

    Mono.Android 基础 (地址) Mono.Android项目结构是 — Project + Assets + Resources + drawable + layout + values R ...

随机推荐

  1. Sqlserver的身份验证模式

    1.服务器名称: . 在服务器名称这里,我们有以下几种选择来连接到本地: (1)默认的服务器名称:也就是电脑主机的名称 (2)".",“.”就表示主机. (3)localhost ...

  2. ubuntu关闭服务需要身份验证

    service tomcat stop ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === 需要通过认证才能停止“tom ...

  3. js document.load 和 document.ready 区别

    document.ready和onload的区别——JavaScript文档加载完成事件 页面加载完成有两种事件 一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件) 二是onloa ...

  4. javascript 重构alert()

    javascript问题,关于重构window.alert()后,然后调用window原本的window.alert()的方法 大神们,问个问题,如果在script标签的第一行散写,重构了window ...

  5. windbg 边学边记attach 进程和open dump的两个方式查看线程的占用cpu资源

    首先我是attach到进程的方式,附加到进程把. vs里边有个远程调试就是通过连接到远程机附加到进程操作的.在 有公网IP情况下挺好用,但涉及到nat穿越之类的,因为用户的不方便设置,这种调试方式也有 ...

  6. oracle主键修改&设置某一字段可以为null

    1.oracle主键修改 1.1)首先查看需要修改的表的主键名,默认的情况下,数据库会自动分配 select * from user_cons_columns where table_name='表名 ...

  7. UOJ 67 新年的毒瘤 - Tarjan

    Description 给出一个无向图, 要求找出某个点$u$, 去掉$u$和$u$所连的边, 所剩下的节点构成一棵树. Solution 首先, 割点肯定是不可能满足条件的, 因为去掉割点后会构成若 ...

  8. python imaplib无痕取信的主要

    typ, data = M.fetch(num, (UID BODY.PEEK[]))  

  9. LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题

    #6004. 「网络流 24 题」圆桌聚餐 内存限制:256 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数 ...

  10. Firebird存储过程--更加人性化的设计

    Firebird存储过程--更加人性化的设计 begin For select house_id,goods_id ,qty from  table1 where id=:VAR_ID into :v ...