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. C++之变量

    变量 **作用**:给一段指定的内存空间起名,方便操作这段内存 **语法**:数据类型 变量名 = 初始值; 语法:数据类型  变量名 = 初始值;   记得加英文分号结束语句 > 注意:C++ ...

  2. leetcode-第10周双周赛-5080-查找两颗二叉搜索树之和

    题目描述: 自己的提交: class Solution: def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -&g ...

  3. leetcode-63-不同路径②

    题目描述: 第一次提交: class Solution: def uniquePathsWithObstacles(self, obstacleGrid) : m = len(obstacleGrid ...

  4. linux安装splunk-enterprise

    一.介绍 Splunk是一款功能强大,功能强大且完全集成的软件,用于实时企业日志管理,可收集,存储,搜索,诊断和报告任何日志和机器生成的数据,包括结构化,非结构化和复杂的多行应用程序日志. 它允许您以 ...

  5. clover无缘无故隐藏书签栏原因

    可能是不小心按住了Ctrl+shift+B

  6. Vue.js Ajax(axios)

    Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求. Axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中. Github开源地址: ht ...

  7. 51nod-1130-N的阶乘的长度V2(斯特林近似)-套斯特林公式

    输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3. 输入 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 1000) 第2 - T + 1行:每 ...

  8. 左神算法进阶班1_5BFPRT算法

    在无序数组中找到第k大的数1)分组,每N个数一组,(一般5个一组)2)每组分别进行排序,组间不排序3)将每个组的中位数拿出来,若偶数,则拿上 / 下中位数, 成立一个一个新数组.4)新数组递归调用BF ...

  9. Vue的组件及传参

    目录 Vue的组件及传参 Vue组件的概念 根组件 子组件(局部组件) 父组件向子组件传值 子组件向父组件传值 Vue的组件及传参 Vue组件的概念 我们首先要知道组件的概念,实际上每一个组件都是一个 ...

  10. python collections模块 之 defaultdict

    defaultdict 是 dict 的子类,因此 defaultdict 也可被当成 dict 来使用,dict 支持的功能,defaultdict 基本都支持.但它与 dict 最大的区别在于,如 ...