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的更多相关文章

  1. Android 基础控件 TextView

    一TextView介绍: TextView是UI最基本的组件,使用TextView可以显示丰富的文本信息.设置添加TextView最常见的方法就是在xml中添加TextView元素,并指定属性.Tex ...

  2. Android基础控件ListView基础操作

    1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...

  3. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  4. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

  5. android 基础控件 EditText

    EditText 简介: EditText 控件继承 TextView ,它有TextView的所有属性和方法,并且自身是可编辑的: extends TextView java.lang.Object ...

  6. android 界面控件 textview 全解

    textview基本使用: <TextView 10. android:id="@+id/txtOne" 11. android:layout_width="200 ...

  7. Android基础控件Button的使用

    1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...

  8. android基础控件的使用

    控件在屏幕上位置的确定 通常情况下控件在屏幕上确定至少要连接两条线(一条水平,一条垂直) 如下图连接了四条线 辅助线 辅助线的调出: 水平辅助线:进入activity.xml的设计模式之后如下图 为了 ...

  9. Android基础控件TextClock和Chronometer的使用

    1.简介 DigitalClock, TextClock,AnalogClock,Chronometer其中DigitalClock和AnalogClock废弃了! TextClock是在Androi ...

随机推荐

  1. Day9, 进程、线程、协程篇

    本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者 ...

  2. jpa简单规则(转https://www.cnblogs.com/rulian/p/6434631.html)

    一.常用规则速查 1  And 并且2  Or  或3  Is,Equals 等于4  Between  两者之间5  LessThan 小于6  LessThanEqual   小于等于7  Gre ...

  3. [MtOI2019]幽灵乐团

    题目 一个很暴力的辣鸡做法 考虑到两个数的\(\gcd\)是所有质数次幂取\(\min\),两个数的\(\rm lcm\)是所有质数次幂取\(\max\),于是最后的答案一定是\(\prod p_i^ ...

  4. 买不到的数目 /// 结论公式 oj26316

    题目大意: 给定a b(这题题意不清 其实a b互质) 设变量x y(x>=0,y>=0),求 x*a+y*b=c 找到最大的不可能达到的c 如a=4 b=7 那么c=14 有这样一个定理 ...

  5. 【python】遇到的错误

    呃.这学期在学python啦.之前虽然自学过,但都是跟着教程也没使用什么编译环境.没遇到奇奇怪怪的错误. 现在就当作一个记录贴吧. 用的编译工具是pycharm.电脑是MacBook Air 1.我在 ...

  6. 网页设计师神器,快速生成网站配色、字型等风格的工具——Stylify Me

    在设计网页时,最重要的一项便是网页的配色,颜色的使用在网页制作中起着非常关键的作用,不同的网站有着自己不同的风格,也有着自己不同的颜色.今天给大家介绍一个在线生成网站配色的工具——Stylify Me ...

  7. 【笔记篇】斜率优化dp(二) SDOI2016征途

    =======传=送=门======= 搜题目名会搜出很多奇怪的东西... 这个题目似乎有点毒? 比如在bzoj和loj上可以1A的代码上会在luogu TLE 2个点, 在cogs TLE 10个点 ...

  8. 【学术篇】SDOI2010 古代猪文

    这里可能包含传送门 又双叒叕数论大杂烩... 定理什么我都不会证 题目很长很啰嗦 但是题意很显然... 化完式子之后就是这么个东东:\(G^{\sum_{k|n}C_k^{\frac{n}{k}}}\ ...

  9. robotframework冷门关键字

    1.Reload Page 模拟页面重载 2.Register Keyword To Run On Failure 参数: Keyword 描述: 当Selenium2Library类库关键字执行失败 ...

  10. golang中使用gorm连接mysql操作

    一.代码 package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/go- ...