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 ...
随机推荐
- 2018今日头条湖北省赛【D】
[题目链接]https://www.nowcoder.com/acm/contest/104/C 不知道这题为啥没过.队友现场推的都是对的..233333好像代码写的有问题,下来就很恼火. 题意大概就 ...
- List<Map>中根据map的同一指标项数据——去重代码
先看网络上,博客经常出现的错误代码: for(ABatchAddCheckVO aBatchAddCheckVO : addList){ dto.put("aac001",aBat ...
- git mac安装
1.git安装包安装 去官网下载最行的git版本 安装即可 https://git-scm.com/download/mac 但是一般的git仓库需要sshkey来做验证 下面奉上具体的命令: 需要生 ...
- drools决策表的使用
决策表我们在drools规则引擎初探里做了简单介绍,这里主要是介绍如何通过java代码来把这个excel文件和drools关联起来,如何使其达到我们想要的效果. 这里假设我们在resources目录下 ...
- 实现虚拟机VMware上Linux与本机windows互相复制与粘贴的问题
解决方法:只需要在Linux系统中安装一个vmware-tools的工具 1.选择虚拟机菜单,有安装vmware tools 工具的选项 点击之后,在Linux的桌面下会出现 VMwareTools. ...
- 十一. for of
const fruits = ['Apple','Banana','Orange','Mango']; es5: 可读性差 for(let i=0; i < fruits.length; i + ...
- leetcode-220-存在重复元素③*
题目描述: 方法一:二叉搜索树+滑动窗口 方法二:桶排序 O(N) class Solution: def containsNearbyAlmostDuplicate(self, nums: List ...
- List、Map、Set 三个接口,存取元素时,各有什么特点
List与Set都是单列元素的集合,它们有一个功共同的父接口Collection. Set里面不允许有重复的元素, 存元素:add方法有一个boolean的返回值,当集合中没有某个元素,此时add方法 ...
- thinkphp 变量输出
在模板中输出变量的方法很简单,例如,在控制器中我们给模板变量赋值: 大理石平台支架 $name = 'ThinkPHP'; $this->assign('name',$name); $this- ...
- echart数据的实时更新