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. Python匹马行天下之面向对象

    概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 面向过程编程最易被初学 ...

  2. 面试系列38 分库分表之后,id主键如何处理?

    (1)数据库自增id 这个就是说你的系统里每次得到一个id,都是往一个库的一个表里插入一条没什么业务含义的数据,然后获取一个数据库自增的一个id.拿到这个id之后再往对应的分库分表里去写入. 这个方案 ...

  3. Linux下mysql实现远程连接

    首先明白一点并不是mysql禁止远程连接,而是MYSQL的账号禁止远程连接.可能觉得我有点咬文嚼字了,不过我感觉分清这点还是很重要的.默认情况下,所有账号都是禁止远程连接的.在安装MYSQL的时候,在 ...

  4. Error: setup script specifies an absolute path

    在安装sklearn的时候,出现: error: Error: setup script specifies an absolute path: /opt/xgboost-0.47/python-pa ...

  5. Windows 子网掩码

    子网掩码(subnet mask)又叫网络掩码.地址掩码.子网络遮罩,它是一种用来指明一个IP地址的哪些位标识的是主机所在的子网,以及哪些位标识的是主机的位掩码.子网掩码不能单独存在,它必须结合IP地 ...

  6. 【转载】99%的人都理解错了HTTP中GET与POST的区别

    作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文链接 作者:WebT ...

  7. luoguP1154 奶牛分厩 [数论]

    题目描述 农夫约翰有N(1<=N<=5000)头奶牛,每头奶牛都有一个唯一的不同于其它奶牛的编号Si,所有的奶牛都睡在一个有K个厩的谷仓中,厩的编号为0到K-1.每头奶牛都知道自己该睡在哪 ...

  8. 欧拉定理+质因子分解+矩阵快速幂——cf1182E

    好题! /* gi=c^i * fi gi=gi-1 * gi-2 * gi-3 把g1,g2,g3质因数分解 g1=p1^e11 * p2^e12 * p3^e13 ... pk^e1k g2=p1 ...

  9. JS 变量的数据类型 运算符

    JS中变量的类型有:数值型.字符型.布尔型.undefined.null.array.object.function 1.数值型:可以进行算术运算的(加.减.乘.除) 数值型包括:整型(整数)和浮点型 ...

  10. HDU-1501-Zipper-字符串的dfs

    Given three strings, you are to determine whether the third string can be formed by combining the ch ...