Android基础控件TextView
1、常用属性
<TextView
android:id="@+id/text11" //组件id
android:layout_width="match_parent" //宽度
android:gravity="center" //内容对齐方式
android:layout_height="100dp" //高度
android:background="@drawable/back" //背景
android:autoLink="web" //web,phone等连接
android:text="www.baidu.com"/> //文本
android:textColor="@color/colorPrimaryDark" //文本颜色
android:focusable="true" //键盘状态下显示焦点
android:focusableInTouchMode="true" //触屏下显示焦点
android:marqueeRepeatLimit="marquee_forever" //重复滚动的次数
android:ellipsize="marquee" //文本显示模式 省略开头、中间、结尾、跑马灯
android:singleLine="true" //是否单行显示
/>
2、简单使用
xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TextViewActivity"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:gravity="center_horizontal"> <TextView
android:id="@+id/text11"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp"
android:background="@drawable/back"
android:autoLink="web"
android:text="www.baidu.com"/>
<TextView
android:id="@+id/text22"
android:layout_centerHorizontal="true"
android:layout_width="150dp"
android:text="你 好"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_below="@id/text11"
android:background="@drawable/shape_back_values"/>
<TextView
android:id="@+id/text33"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp"
android:layout_below="@id/text22"
android:layout_marginTop="5dp"
android:background="#ffffff"/> <TextView
android:id="@+id/text44"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp"
android:layout_below="@id/text33"
android:layout_marginTop="5dp"
android:background="@drawable/back"/>
<TextView
android:id="@+id/text55"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="100dp"
android:layout_below="@id/text44"
android:textColor="@color/colorPrimaryDark"
android:layout_marginTop="5dp"
android:background="#e2e2e2"
android:text="手机卡打飞机as类方法的看反馈的反馈打开了反馈的反馈上课啦反馈代理费路径"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:ellipsize="marquee"
android:singleLine="true"
/> </RelativeLayout>
</android.support.constraint.ConstraintLayout>
Java文件
public class TextViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_view);
TextView t3 = (TextView)findViewById(R.id.text33);
SpannableString span = new SpannableString("红色打电话斜体删除线绿色下划线图片:.");
span.setSpan(new ForegroundColorSpan(Color.RED),0,2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new URLSpan("tel:1421323123"),2,5,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),5,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new StrikethroughSpan(),7,10,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new UnderlineSpan(),10,16,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new ForegroundColorSpan(Color.GREEN),10,13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Drawable d = ContextCompat.getDrawable(this,R.drawable.back);
d.setBounds(0,0,50,50);
ImageSpan imgSpan = new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
span.setSpan(imgSpan,18,19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
t3.setText(span);
// BackgroundColorSpan 背景色
// ClickableSpan 文本可点击,有点击事件
// ForegroundColorSpan 文本颜色(前景色)
// MaskFilterSpan 修饰效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)
// MetricAffectingSpan 父类,一般不用
// RasterizerSpan 光栅效果
// StrikethroughSpan 删除线(中划线)
// SuggestionSpan 相当于占位符
// UnderlineSpan 下划线
// AbsoluteSizeSpan 绝对大小(文本字体)
// DynamicDrawableSpan 设置图片,基于文本基线或底部对齐。
// ImageSpan 图片
// RelativeSizeSpan 相对大小(文本字体)
// ReplacementSpan 父类,一般不用
// ScaleXSpan 基于x轴缩放
// StyleSpan 字体样式:粗体、斜体等
// SubscriptSpan 下标(数学公式会用到)
// SuperscriptSpan 上标(数学公式会用到)
// TextAppearanceSpan 文本外貌(包括字体、大小、样式和颜色)
// TypefaceSpan 文本字体
// URLSpan 文本超链接
TextView t4 = (TextView)findViewById(R.id.text44);
StringBuilder sb = new StringBuilder();
for (int i=0;i<20;i++){
sb.append("好友" + i + ",");
}
String user =sb.substring(0,sb.lastIndexOf(",")).toString();
t4.setMovementMethod(LinkMovementMethod.getInstance());
t4.setText(addClickPart(user),TextView.BufferType.SPANNABLE);
}
//定义一个点击每个部分文字的处理方法
private SpannableStringBuilder addClickPart(String str) {
//赞的图标,这里没有素材,就找个笑脸代替下~
Drawable d = ContextCompat.getDrawable(this,R.drawable.back);
d.setBounds(0,0,50,50);
ImageSpan imgspan = new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
SpannableString spanStr = new SpannableString("p.");
spanStr.setSpan(imgspan, 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
//创建一个SpannableStringBuilder对象,连接多个字符串
SpannableStringBuilder ssb = new SpannableStringBuilder(spanStr);
ssb.append(str);
String[] likeUsers = str.split(",");
if (likeUsers.length > 0) {
for (int i = 0; i < likeUsers.length; i++) {
final String name = likeUsers[i];
final int start = str.indexOf(name) + spanStr.length();
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast.makeText(TextViewActivity.this, name,
Toast.LENGTH_SHORT).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
//删除下划线,设置字体颜色为蓝色
ds.setColor(Color.BLUE);
ds.setUnderlineText(false);
}
},start,start + name.length(),0);
}
}
return ssb.append("等" + likeUsers.length + "个人觉得很赞");
}
}
效果图

Android基础控件TextView的更多相关文章
- Android 基础控件 TextView
一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...
- Android基础控件ListView基础操作
1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...
- android 基础控件(EditView、SeekBar等)的属性及使用方法
android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...
- 矩阵, 矩阵 , Android基础控件之ImageView
天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...
- android 基础控件 EditText
EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...
- android 界面控件 textview 全解
textview基本使用: <TextView 10. android:id="@+id/txtOne" 11. android:layout_width="200 ...
- Android基础控件Button的使用
1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...
- android基础控件的使用
控件在屏幕上位置的确定 通常情况下控件在屏幕上确定至少要连接两条线(一条水平,一条垂直) 如下图连接了四条线 辅助线 辅助线的调出: 水平辅助线:进入activity.xml的设计模式之后如下图 为了 ...
- Android基础控件TextClock和Chronometer的使用
1.简介 DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了! TextClock是在Androi ...
随机推荐
- Vue项目的配置项
目录 Vue项目的配置项 配置项 加载全局css文件 加载全局js文件 store仓库的配置和简单用法 BootStrap环境和jQuery的配置 前端后端交互(CORS问题) axios配置项(前端 ...
- UVA 12304 /// 圆的综合题 圆的模板
题目大意: ①给出三角形三个点,求三角形外接圆,求外接圆的圆心和半径. ②给出三角形三个点,求三角形内接圆,求内接圆的圆心和半径. ③给出一个圆,和一个点,求过该点的圆的切线与x轴的夹角(0<= ...
- String 字符串对象
String是什么 String字符串,是一种引用数据类型,并不是基础数据类型. 对于基础数据类型和引用数据类型的区别: 基础数据类型,在创建时直接将值存放在栈内存中. 引用数据类型,在创建时栈内存中 ...
- 【转】Windows(server2008)下使用VisualSVN Server搭建SVN服务器
参考文献 1.Windows下使用VisualSVN Server搭建SVN服务器(百度经验) 挺好就是没有配图已验证可用 2.在Windows Server 2008上部署SVN代码管理器 把第二 ...
- 2019-8-31-C#-字典-Dictionary-的-TryGetValue-与先判断-ContainsKey-然后-Get-的性能对比
title author date CreateTime categories C# 字典 Dictionary 的 TryGetValue 与先判断 ContainsKey 然后 Get 的性能对比 ...
- Ansible的copy模块批量下发文件
copy模块的参数,ansible 主机组 -m copy -a '' src: 指定源文件或目录 dest: 指定目标服务器的文件或目录 backup: 是否要备份 owner: 拷贝到目标服务器后 ...
- Linux-iptables-route-rule
详情: http://www.mamicode.com/info-detail-1412618.html 最后面有粘贴 linux系统路由表 linux一共0-255个路由表 linux可以自定义从 ...
- scala中Tuple简单使用
/** * Tuple简单使用记录 * 最大22个参数 */ object TupleUse { def main(args: Array[String]): Unit = { // 简单Tuple ...
- php 数据导出到excel 2种带有合并单元格的导出
具体业务层面 可能会有所不同.以下两种方式涉及的合并单元格地方有所不同,不过基本思路是一致的. 第一种是非插件版本.可能更容易理解点,基本思路就是 组装table 然后 读取 输出到excel上.缺点 ...
- C++仿函数和回调函数的异同
C++回调函数(callback)与仿函数(functor)的异同 c++仿函数 functor C++仿函数和回调函数的异同